nathbns commited on
Commit
3355ecd
·
verified ·
1 Parent(s): b6bd9ea

Update train_tensorflow.py

Browse files
Files changed (1) hide show
  1. train_tensorflow.py +8 -14
train_tensorflow.py CHANGED
@@ -1,9 +1,3 @@
1
- # This module trains the CNN based on the labels provided in ./data/CNN
2
- # Note that data must be first split into train, validation, and test data
3
- # by running split_data.py.
4
- # Reference:
5
- # https://towardsdatascience.com/a-single-function-to-streamline-image-classification-with-keras-bd04f5cfe6df
6
-
7
  from matplotlib import pyplot as plt
8
  from tensorflow.keras.preprocessing.image import ImageDataGenerator
9
  from tensorflow.keras.models import Sequential
@@ -148,10 +142,9 @@ def test_model(model):
148
  Does not return anything.'''
149
  testdir = DATA_FOLDER + 'test'
150
 
151
- # pieces = ['Empty', 'Rook', 'Knight', 'Bishop', 'Queen', 'Pawn', 'King']
152
- pieces = ['Empty', 'Rook_White', 'Rook_Black', 'Knight_White', 'Knight_Black', 'Bishop_White',
153
- 'Bishop_Black', 'Queen_White', 'Queen_Black', 'King_White', 'King_Black', 'Pawn_White', 'Pawn_Black']
154
- pieces.sort()
155
  score = 0
156
  total_size = 0
157
  for subdir, dirs, files in os.walk(testdir):
@@ -160,7 +153,8 @@ def test_model(model):
160
  continue
161
  piece = subdir.split('/')[-1]
162
  path = os.path.join(subdir, file)
163
- y_prob = model.predict(cv2.imread(path).reshape(1, 300, 150, 3))
 
164
  y_pred = y_prob.argmax()
165
  if y_pred < 0 or y_pred >= len(pieces):
166
  print(y_pred, y_prob)
@@ -176,7 +170,7 @@ if __name__ == '__main__':
176
  history = fit_model(model, train_generator,
177
  validation_generator, save=False)
178
  save_history(history, "./history.json")
179
- plot_accuracy(history)
180
- plot_loss(history)
181
  test_model(model)
182
- model.save_weights('./model_weights.h5')
 
 
 
 
 
 
 
1
  from matplotlib import pyplot as plt
2
  from tensorflow.keras.preprocessing.image import ImageDataGenerator
3
  from tensorflow.keras.models import Sequential
 
142
  Does not return anything.'''
143
  testdir = DATA_FOLDER + 'test'
144
 
145
+ # IMPORTANT: Ordre EXACT comme dans le générateur (alphabétique)
146
+ pieces = ['Bishop_Black', 'Bishop_White', 'Empty', 'King_Black', 'King_White', 'Knight_Black',
147
+ 'Knight_White', 'Pawn_Black', 'Pawn_White', 'Queen_Black', 'Queen_White', 'Rook_Black', 'Rook_White']
 
148
  score = 0
149
  total_size = 0
150
  for subdir, dirs, files in os.walk(testdir):
 
153
  continue
154
  piece = subdir.split('/')[-1]
155
  path = os.path.join(subdir, file)
156
+ img = cv2.imread(path).reshape(1, 300, 150, 3) / 255.0
157
+ y_prob = model.predict(img, verbose=0)
158
  y_pred = y_prob.argmax()
159
  if y_pred < 0 or y_pred >= len(pieces):
160
  print(y_pred, y_prob)
 
170
  history = fit_model(model, train_generator,
171
  validation_generator, save=False)
172
  save_history(history, "./history.json")
173
+ # plot_accuracy(history)
174
+ # plot_loss(history)
175
  test_model(model)
176
+ model.save_weights('./model_weights.weights.h5')