-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathModel_ResNet_milestone.py
84 lines (63 loc) · 2.58 KB
/
Model_ResNet_milestone.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
import numpy as np
from keras import layers
from keras.layers import Input, Add, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2D, AveragePooling2D, MaxPooling2D, GlobalMaxPooling2D
from keras.models import Model, load_model
from keras.preprocessing import image
from keras.utils import layer_utils
from keras.utils.data_utils import get_file
from keras.applications.imagenet_utils import preprocess_input
# import pydot
from IPython.display import SVG
from keras.utils.vis_utils import model_to_dot
from keras.utils import plot_model
#from resnets_utils import *
from keras.initializers import glorot_uniform
import scipy.misc
from matplotlib.pyplot import imshow
import keras.backend as K
from Model_ResNet_helper import *
from preprocessing import load_images as l_i
from preprocessing import load_file as l_f
from PIL import Image
from os import listdir
import random
np.random.seed(1)
data_file_name = "sampled_data.csv"
path_training = "train/images/" # path from current folder to training images
path_testing = "train/images/" # path from current folder to testing images
### Set training and testing details #################################################
m = l_f.check_num_images(data_file_name)
m_training = 30000
m_testing = 1000
num_epochs = 100
num_batches = 16
### Train Data #######################################################################
### Shuffle the m examples
list_of_rows = np.arange(1, m+1, 1)
np.random.shuffle(list_of_rows)
### Take a random m_training examples and load the data
img_dict = l_f.load_file(data_file_name, list_of_rows[:m_training])
### Load and process the images
(train_x, train_y) = l_i.load_images(path_training, img_dict)
### Normalize image vectors
train_x = train_x / 255
### Convert to one hot vector
print(train_x.shape)
print(train_y.shape)
### Test Data #######################################################################
### Take a random m_testing examples and load the data
img_dict = l_f.load_file(data_file_name, list_of_rows[m_training:m_training + m_testing])
### Load and process the images
(test_x, test_y) = l_i.load_images(path_testing, img_dict)
### Normalize image vectors
test_x = test_x / 255
### Convert to one hot vector
print(test_x.shape)
print(test_y.shape)
model = ResNet50(input_shape = (64, 64, 3)) #changed classes from 4 to 2
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.fit(train_x, train_y, epochs = num_epochs, batch_size = num_batches)
preds = model.evaluate(test_x, test_y)
print(preds)
print ("Loss = " + str(preds[0]))
print ("Test Accuracy = " + str(preds[1]))