indent preformatted text by 4 spaces
import numpy as np
import tensorflow as tf
from PIL import Image
from tensorflow.keras import Model,Sequential
from tensorflow.keras.layers import Conv2D,MaxPooling2D,Flatten,Dense,Dropout,
from tensorflow.keras.applications.vgg16 import VGG16
from sklearn.model_selection import train_test_split
from keras_preprocessing.image import ImageDataGenerator
from PIL import Image
import os.path
import glob
import random
image_path=glob.glob('gar/*/*.jpg')
label_type=[image_p.split('\\')[1] for image_p in image_path]
labels=np.unique(label_type)
index_to_label=dict((l,n)for l,n in enumerate(labels))
label_to_index=dict((n,l)for l,n in index_to_label.items())
all_labels=[label_to_index.get(name)for name in label_type]
random_index=np.random.permutation(len(image_path))
image_path=np.array(image_path)[random_index]
all_labels=np.array(all_labels)[random_index]
sep=int(len(image_path)*0.7)
train_image_path=image_path[:sep]
train_y=all_labels[:sep]
test_image_path=image_path[sep:]
test_y=all_labels[sep:]
train=tf.data.Dataset.from_tensor_slices((train_image_path,train_y))
test=tf.data.Dataset.from_tensor_slices((test_image_path,test_y))
def load_pic(path,label):
image=tf.io.read_file(path)
image=tf.image.decode_jpeg(image,channels=3)
image=tf.image.resize(image,[256,256])
image=tf.cast(image,tf.float32)
image=image/255
return image,label
train=train.map(load_pic)
train=train.shuffle(1000).batch(64)
test=test.map(load_pic)
test=test.shuffle(1000).batch(64)
def get_model():
gmodel=Sequential([
Conv2D(64, (3, 3), activation='relu', input_shape=(256,256,3)),
Conv2D(64,(3,3),activation='relu'),
MaxPooling2D((1,1),strides=2,padding='same'),
Conv2D(128, (3, 3), activation='relu'),
Conv2D(128, (3, 3), activation='relu'),
MaxPooling2D((1,1), strides=2, padding='same'),
Conv2D(256, (3, 3), activation='relu'),
Conv2D(256, (3, 3), activation='relu'),
MaxPooling2D((1,1), strides=2, padding='same'),
Conv2D(512, (3, 3), activation='relu'),
Conv2D(512, (3, 3), activation='relu'),
MaxPooling2D((1,1), strides=2, padding='same'),
Conv2D(512, (3, 3), activation='relu'),
Conv2D(512, (3, 3), activation='relu'),
MaxPooling2D((1,1), strides=2, padding='same'),
Flatten(),
Dense(256),
Dense(3, activation='softmax')
])
return gmodel
model=get_model()
model.compile(
optimizer=tf.keras.optimizers.Adam(0.0001),
loss="categorical_crossentropy",
metrics=['accuracy']
)
history=model.fit(train,epochs=5,validation_data=test)
I dont know why when I try to train this model to classify picture into four catagories, the accuracy only about 25%, and I have tried change model, loss function and optimizer but all of these do not not. Can some one tell me how to solve this problems. Thank very much