I am trying to modify and calling my own model from this website.
but here is my question.
def prepare(filepath):
IMG_SIZE = 70 # 50 in txt-based
img_array = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE) # read in the image, convert to grayscale
new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE)) # resize image to match model's expected sizing
return new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 1) # return the image with shaping that TF wants.
My model input is (180x180x3) , and I can't change it to grayscale due to index out of range.
Since I know my channel is 3, I would like to change my array to
new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 3)
, but when it predict in
print(prediction[0][0])
it is not number 0 or 1, so I can't predict my picture.
Please help me to figure out what happened, no matter question 1 or 2.
I appreciate all of your help.
I expect only 1 or 0, so I can classify label "Pass" or "Fail"
CodePudding user response:
In the prepare function, the image is being read in as grayscale and then being resized to (IMG_SIZE, IMG_SIZE). If your model expects 3 channels (RGB) but the image is being converted to grayscale (1 channel), then the model will not be able to process the image correctly and you will not get the expected output.
To fix this issue, you can change the following line:
img_array = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE) # read in the image, convert to grayscale
to:
img_array = cv2.imread(filepath) # read in the image
This will read in the image with 3 channels (RGB).
Regarding the second issue, if the prediction is not 0 or 1, then it is likely that the model is not outputting a binary classification. You can try to check the output of the model to see what it is predicting. You can do this by printing out the output of the model before the argmax operation is applied.
If the model is not outputting a binary classification, you may need to modify the model or use a different model that is designed for binary classification.