-
Notifications
You must be signed in to change notification settings - Fork 4
/
5. Train-2.py
100 lines (72 loc) · 2.84 KB
/
5. Train-2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.preprocessing import image
from keras.callbacks import EarlyStopping
from keras.callbacks import ModelCheckpoint
from keras.preprocessing.image import ImageDataGenerator
import pandas as pd
import numpy as np
from tqdm import tqdm
import matplotlib.pyplot as plt
import cv2
from keras import models
from keras import layers
from keras import optimizers
from PIL import Image
import sklearn
from sklearn.model_selection import train_test_split
from statistics import mean
def arrange_data(df):
image_data = []
img_paths = np.asarray(df.iloc[:, 0]) #First column is the image paths
for i in tqdm(range(len(img_paths))):
img = image.load_img(img_paths[i],target_size=(200, 150, 3))
img = image.img_to_array(img)
img = img/255
image_data.append(img)
X = np.array(image_data)
Y = np.array(df.iloc[:,1:29])
print("Shape of images:", X.shape)
print("Shape of labels:", Y.shape)
return X, Y
print("Processing train..")
train_df = pd.read_csv("Train.csv", delimiter=" ")
X_train, Y_train = arrange_data (train_df)
print("Processing valid..")
val_df = pd.read_csv("Valid.csv", delimiter=" ")
X_val, Y_val = arrange_data (val_df)
num_classes = 28 # Types of Genres
from keras.applications import VGG16
#Load the VGG model
vgg_conv = VGG16(weights='imagenet', include_top=False, input_shape=(200,150, 3))
# Freeze the layers except the last 4 layers
for layer in vgg_conv.layers[:-4]:
layer.trainable = False
# Create the model
model = models.Sequential()
# Add the vgg convolutional base model
model.add(vgg_conv)
num_classes = 28
# Add new layers
model.add(layers.Flatten())
model.add(layers.Dense(1024, activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(num_classes, activation='sigmoid'))
# Show a summary of the model. Check the number of trainable parameters
model.summary()
#model.compile(loss='binary_crossentropy',
#optimizer=keras.optimizers.Adagrad(),
#metrics=['accuracy'])
#model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.compile(optimizer=optimizers.RMSprop(lr=1e-4), loss='binary_crossentropy', metrics=['accuracy'])
# construct the training image generator for data augmentation
aug = ImageDataGenerator(rotation_range=20, zoom_range=0.15,width_shift_range=0.2, height_shift_range=0.2,
shear_range=0.15,horizontal_flip=True, fill_mode="nearest")
# train the network
EPOCHS=50
BS = 64
history = model.fit_generator(aug.flow(X_train, Y_train, batch_size=BS),validation_data=(X_val, Y_val),
steps_per_epoch=len(X_train) // BS, epochs=EPOCHS)
model.save('Model_4d.h5')