On this tutorial, I’ll present how you can construct a meals picture classifier, utilizing switch studying.. The objective is to take meals photos as enter, and routinely determine what dish it’s.. e.g. pizza, noodles, burger, sandwich and many others …
Step 1: First, create a listing say ‘food-images’ or ‘food-samples’. On this listing, create a sub-folder for every class: {pizza, noodles, burger, soup, sandwich ..} and many others. Every sub-folder ought to comprise photos (jpg, png, jpeg) for that class.
Step 2: Now, load this knowledge into python. For this, we use operate ‘image_dataset_from_directory’ from keras.utils.
train_data, valid_data = keras.utils.image_dataset_from_directory(
"food-images",
labels = "inferred",
class_names=('burger', 'cake', 'juice', 'noodles', 'pizza'),
image_size=(224,224),
batch_size=32,
validation_split=0.2,
subset="each",
seed=1337)
Step 3: Load pre-trained mannequin weights. Word, these fashions are already educated on very giant scale datasets, so even when our new picture pattern for meals photos is comparatively very small (say few 100 to 1000 photos per class), our classifier can nonetheless obtain first rate accuracy. That’s the great thing about switch studying, the place we make the most of mannequin educated on giant dataset (say to determine cats, canines, horse, airplane, automotive, bus from picture) and modify or customise it for a brand new activity.
# load pre-trained mannequin
from keras.purposes.resnet50 import ResNet50
from keras.purposes.resnet50 import preprocess_inputbase_model = ResNet50(weights='imagenet', include_top=False)
base_model.trainable = False
Step 4: Now, we add some new layers on high of this base_model, and solely prepare these new layers on our meals picture dataset.
# construct new mannequin on high of pre-trained mannequinfrom keras import layers
num_classes = 5
# add new layers to base_model
mannequin = keras.Sequential([
base_model,
layers.GlobalAveragePooling2D(),
layers.Dense(64, activation='relu'),
layers.Dense(num_classes, activation='softmax')
])
Step 5: Now, we simply compile our mannequin as ordinary, and prepare it on new meals picture dataset. Word, in step 3, we set base_model.trainable as False, so our pre-trained mannequin just isn’t be altered throughout coaching. Solely the brand new layers added on high are educated on customized dataset.
mannequin.compile(optimizer='adam', loss='sparse_categorical_crossentropy')
mannequin.match(train_data, validation_data=valid_data, epochs=3)
Step 6: Let’s attempt our food-image classifier on few pattern take a look at photos.
# attempt mannequin on new pattern picture
from matplotlib import pictureplt.determine(figsize=(7,7))
for okay in vary(12):
plt.subplot(3, 4, okay+1)
test_img_file = str(okay+1) + ".jpg"
test_img = picture.imread(test_img_file)
plt.imshow(test_img)
y = mannequin.predict(np.array([test_img]))
label = class_labels[np.argmax(y[0])]
plt.xlabel(label)
plt.xticks([])
plt.yticks([])
Yumm, isn’t it? For extra yummy code examples, take a look at my git repo at : https://github.com/pamruta/Keras