Skip to content

Instantly share code, notes, and snippets.

@abhisheksoni27
Created August 29, 2018 13:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhisheksoni27/184c49ca703eb124e1b17eb8dd8af518 to your computer and use it in GitHub Desktop.
Save abhisheksoni27/184c49ca703eb124e1b17eb8dd8af518 to your computer and use it in GitHub Desktop.
import tensorflow as tf
from tensorflow import keras
from tensorflow.python.keras.applications.inception_resnet_v2 import preprocess_input
from tensorflow.python.keras.models import load_model
from tensorflow.python.keras.applications.inception_resnet_v2 import InceptionResNetV2
from tensorflow.python.keras.applications.mobilenet import preprocess_input
from tensorflow.python.keras.applications.mobilenet import MobileNet
from tensorflow.python.keras.preprocessing.image import ImageDataGenerator
import numpy as np
import cv2
import matplotlib.pyplot as plt
import glob
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Flatten, Conv2D, Dropout, MaxPooling2D
from tensorflow.python.keras.models import Model
mobile = MobileNet()
x = mobile.layers[-6].output
prediction = Dense(2, activation="softmax")(x)
model = Model(inputs=mobile.input, outputs=prediction)
for layer in model.layers[:-5]:
layer.trainable = False
image_size = 224
batch_size = 32
data_generator = ImageDataGenerator(
width_shift_range=0.2,
height_shift_range=0.2,
rescale=1./255)
test_datagen = ImageDataGenerator( rescale=1./255)
train_generator = data_generator.flow_from_directory(
'./dataset/training',
target_size=(image_size, image_size),
batch_size=batch_size,
class_mode='categorical')
validation_generator = test_datagen.flow_from_directory(
'./dataset/testing',
target_size=(image_size, image_size),
batch_size=batch_size,
class_mode='categorical')
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
with tf.device('/gpu:0'):
model.fit_generator(train_generator,
steps_per_epoch= 8251 // batch_size,
epochs=3,
validation_data=validation_generator,
validation_steps=2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment