Commit d96ae90d authored by Fernando P.W.N's avatar Fernando P.W.N

Update dynamic sign classifier

parent 1aaff404
import keras
from keras.models import Sequential
from keras.applications.vgg16 import VGG16
from keras.layers import Dense, InputLayer, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D, GlobalMaxPooling2D
from keras.preprocessing import image
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from tqdm import tqdm
from sklearn.model_selection import train_test_split
train = pd.read_csv('UCF/train_new.csv')
train.head()
# creating an empty list
train_image = []
# for loop to read and store frames
for i in tqdm(range(train.shape[0])):
# loading the image and keeping the target size as (224,224,3)
img = image.load_img('UCF/' + train['image'][i], target_size=(480, 272, 3))
# converting it to array
img = image.img_to_array(img)
# normalizing the pixel value
img = img / 255
# appending the image to the train_image list
train_image.append(img)
# converting the list to numpy array
X = np.array(train_image)
# separating the target
y = train['class']
# creating the training and validation set
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42, test_size=0.2, stratify = y)
# separating the target
y = train['class']
# creating the training and validation set
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42, test_size=0.2, stratify = y)
# creating dummies of target variable for train and validation set
y_train = pd.get_dummies(y_train)
y_test = pd.get_dummies(y_test)
# creating the base model of pre-trained VGG16 model
base_model = VGG16(weights='imagenet', include_top=False)
# extracting features for training frames
X_train = base_model.predict(X_train)
X_train.shape
# extracting features for validation frames
X_test = base_model.predict(X_test)
# reshaping the training as well as validation frames in single dimension
X_train = X_train.reshape(522, 15*8*512)
X_test = X_test.reshape(131, 15*8*512)
# normalizing the pixel values
max = X_train.max()
X_train = X_train/max
X_test = X_test/max
#defining the model architecture
model = Sequential()
model.add(Dense(1024, activation='relu', input_shape=(61440,)))
model.add(Dropout(0.5))
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(8, activation='softmax'))
# defining a function to save the weights of best model
from keras.callbacks import ModelCheckpoint
mcp_save = ModelCheckpoint('weight.hdf5', save_best_only=True, monitor='val_loss', mode='min')
# compiling the model
model.compile(loss='categorical_crossentropy',optimizer='Adam',metrics=['accuracy'])
# training the model
model.fit(X_train, y_train, epochs=200, validation_data=(X_test, y_test), callbacks=[mcp_save], batch_size=128)
model.save_weights('model_saved.h5')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment