'why I can not get the accuract result after perfect training on resnet50 model?

I trained the Keras ResNet50 to image recognition, 20 classes, each class has about 200 augmented images. Train the model, the results looks great. The train accuracy and the val accuracy are almost 100%. But when I choose some train file from the train_ds and some files from the val_ds, the predicit results are bad. Does anyone help me? Many thanks! sorry missing the code files. now I upload the files by github. please check them out. https://github.com/CallaDai/Resnet-50

the model accuracy with epoch the model accuracy with epoch

the model accuracy the model accuracy

the result the result

my target is 7094Z_W_E_0_165123763593117_imgaug.jpg which belongs to train_ds. but the pred result is 7090 class, another class. the detail model file:

the train test file: the train test

the screenshot

enter image description here

the val test file: the val test the screenshot

the screenshot

the resnet-50 code:

data_dir = r"/root/data_Camera/20classWooden_1/"
type(data_dir)
data_dir = pathlib.Path(data_dir)
type(data_dir)
rock = list(data_dir.glob('7094Z_W_E_0/*'))
print(rock[0])
PIL.Image.open(str(rock[0]))
img_height, img_width = 224,224
batch_size = 32
trains_ds = tf.keras.preprocessing.image_dataset_from_directory(
  data_dir,
  validation_split = 0.2,
  subset = "training",
  seed = 42,
  label_mode = 'categorical',
  image_size = (img_height, img_width),
  batch_size = batch_size)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
    data_dir,
    validation_split=0.2,
    subset="validation",
    seed=42,
    label_mode = 'categorical',
    image_size=(img_height, img_width),
    batch_size=batch_size)

class_names = trains_ds.class_names

resnet_model = Sequential()
pretrained_model = tf.keras.applications.ResNet50(include_top=False, 
                                                  input_shape=(224,224,3),
                                                  pooling='avg', 
                                                  classes = 20, 
                                                  weights = 'imagenet')

for layer in pretrained_model.layers: 
    layer.trainable=False
resnet_model.add(pretrained_model)
resnet_model.add(Flatten())
resnet_model.add(Dense(512, activation='relu'))
resnet_model.add(Dense(20,activation='softmax'))
resnet_model.summary()
resnet_model.compile(optimizer=Adam(learning_rate=0.001),loss='categorical_crossentropy',metrics=['accuracy'])
epochs = 5
history= resnet_model.fit(
    trains_ds,
    validation_data=val_ds,
    epochs=epochs)
image = cv2.imread(str(rock[0]))
image_resized = cv2.resize(image,(img_height,img_width))
print(img_height)
print(img_width)
image=np.expand_dims(image_resized,axis=0)
print(image.shape)
pred=resnet_model.predict(image)
print(pred)
output_class=class_names[np.argmax(pred)]
print("The predicted class is", output_class)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source