Created
May 6, 2018 01:27
-
-
Save limitpointinf0/1ef0f1f41b0c5b16b1605e36d4d4ba6f to your computer and use it in GitHub Desktop.
Image Classification with Keras CNN
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np # linear algebra | |
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv) | |
import os | |
from PIL import Image | |
import matplotlib.pyplot as plt | |
from matplotlib.pyplot import imshow | |
from skimage import transform | |
from sklearn.model_selection import train_test_split | |
import traceback | |
from sklearn.utils import shuffle | |
from keras.models import Sequential,Input,Model | |
from keras.layers import Dense, Dropout, Flatten | |
from keras.layers import Conv2D, MaxPooling2D | |
from keras.layers.normalization import BatchNormalization | |
from keras.layers.advanced_activations import LeakyReLU | |
from keras.optimizers import Adam, SGD | |
from keras.callbacks import EarlyStopping | |
def imgToDF(path): | |
"""Takes the root directory for the categorical subdirectories of images and returns a dataframe classifying each path. | |
For example path may contain several directories: | |
Cat, Dog, etc. | |
The function will take path and return: | |
Path Type | |
C:/../cat1.jpg Cat | |
C:/../cat2.jpg Cat | |
... | |
C:/../dog1.jpg Dog | |
""" | |
df = [] | |
for i in os.listdir(path): | |
img_path = path + '/{}'.format(i) | |
img_list = os.listdir(img_path) | |
img_cat = [(path + '/' + i + '/' + x, i) for x in img_list] | |
df += img_cat | |
df = pd.DataFrame.from_records(df, index = list(range(len(df))), columns=['path', 'type']) | |
return df | |
def mk_img_array(df, img_size=(100,100)): | |
"""Take each path generated by imgToDF and construct X data comprised of numpy arrays of dimensions img_size""" | |
X = [] | |
for i in df['path']: | |
img = Image.open(i) | |
img.load() | |
img = np.asarray(img, dtype='float32') | |
img = img/255 | |
data = transform.resize(img,img_size) | |
X.append(data) | |
return np.array(X) | |
def simple_CNN(input_shape, nodes_per=[32,64], hidden=1, out=2, act_out='softmax', act_hid='relu', drop=True, d_rate=0.1): | |
"""generate a simple CNN using Keras with variable architecture and print out model summary""" | |
model = Sequential() | |
model.add(Conv2D(32, (3, 3), padding='same', activation=act_hid, input_shape=input_shape)) | |
model.add(MaxPooling2D(pool_size=(2, 2))) | |
if drop: | |
model.add(Dropout(d_rate)) | |
try: | |
if hidden != 0: | |
for i,j in zip(range(hidden), nodes_per[1:]): | |
model.add(Conv2D(64, (3, 3), padding='same', activation=act_hid)) | |
model.add(MaxPooling2D(pool_size=(2, 2))) | |
if drop: | |
model.add(Dropout(d_rate)) | |
model.add(Flatten()) | |
model.add(Dense(512, activation=act_hid)) | |
model.add(Dropout(0.5)) | |
model.add(Dense(out, activation=act_out)) | |
print(model.summary()) | |
return model | |
except: | |
print(traceback.format_exc()) | |
print('Error in generating hidden layers') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment