Reputation: 11
so i'm trying to test my trained model(image classificator)
tl;dr i have 2 types of photos (20x20 px). 1st type is photos with crushed airplane and 2nd is photos with no crushed airplane (photos taken from the sky)
I am given csv file which contains file names and labels (1 - airplane is on the photo and 0 - no airplane)
This is what i'm doing:
import tensorflow as tf
import pandas as pd
from tensorflow import keras
def read_image(image_file, label):
image = tf.io.read_file(directory+image_file)
image = tf.image.decode_image(image, channels=3, dtype=tf.float32)
return image, label
def prepare_for_test(filepath):
img_array = tf.io.read_file(filepath)
img_array = tf.image.decode_image(img_array, channels=3, dtype=tf.float32)
return img_array
Here's the way i'm creating tf dataset using csv file
directory = 'avia-train/'
df = pd.read_csv(directory+'train.csv')
df['filename'] = df['filename'].apply(lambda x: x+'.png')
filenames = df['filename'].values
signs = df['sign'].values
ds_train = tf.data.Dataset.from_tensor_slices((filenames, signs))
ds_train = ds_train.map(read_image).batch(32)
My model:
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(16, (3,3), activation='relu', input_shape=(20, 20, 3)),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(512, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(
optimizer=keras.optimizers.Adam(),
loss=[
keras.losses.BinaryCrossentropy(),
],
metrics=['accuracy'],
)
model.fit(ds_train,
epochs=5,
verbose=1)
As I understand training goes fine
Here is what I get
Epoch 1/5
972/972 - 45s - loss: 0.2656 - accuracy: 0.8853
Epoch 2/5
972/972 - 7s - loss: 0.1417 - accuracy: 0.9447
Epoch 3/5
972/972 - 7s - loss: 0.1191 - accuracy: 0.9543
Epoch 4/5
972/972 - 7s - loss: 0.1030 - accuracy: 0.9608
Epoch 5/5
972/972 - 8s - loss: 0.0921 - accuracy: 0.9657
And after that I'm trying to use my model
prediction = model.predict([prepare_for_test('avia-test/00a90c41-965e-45d0-90c2-391e20cb25b7.png')])
print(prediction)
And this is what I get
ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 20, 20, 3), found shape=(None, 20, 3)
I've tryed to find something here:
ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=2. Full shape received: [None, 2584]
ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [8, 28, 28]
But for me there is nothing useful
It will be great if you can suggest simple solution but I'll be greatful for any help
Upvotes: 0
Views: 567
Reputation: 11
I found the solution, the prepare_for_test function needs to be changed.
import cv2
def prepare_for_test(filepath):
IMG_SIZE = 20 # my pics are 20x20 px
#if your pics are grayscaled you should use cv2.IMREAD_GRAYSCALE
img_array = cv2.imread(filepath, cv2.IMREAD_COLOR)
new_array = cv2.resize(img_array,(IMG_SIZE,IMG_SIZE))
# if your pics are grayscaled you should use (-1, IMG_SIZE, IMG_SIZE,1)
return new_array.reshape(-1, IMG_SIZE, IMG_SIZE,3)
After that change everything works fine
Upvotes: 1