Example #1
0
 def __init__(self):
     '''
     Initialize
     '''
     self.fname = None
     self.imgs = None
     self.model = CNN_model.CNN()
     self.patch_size = (32, 32)
     self.app = QtWidgets.QApplication(sys.argv)
     self.MainWindow = QtWidgets.QMainWindow()
     self.ui = mainwindow.Ui_MainWindow()
     self.ui.setupUi(self.MainWindow)
     self.MainWindow.setFixedSize(self.MainWindow.width(), self.MainWindow.height())
     self.ui.image_label.setScaledContents(True)
     self.ui.image_label.setMargin(5)
     self.ui.path_button.clicked.connect(self.path_button_click)
     self.ui.patch_button.clicked.connect(self.patch_button_click)
     #self.ui.patch_button.clicked.connect(self.simple_patch_button_click)
     self.ui.go_button.clicked.connect(self.go_button_click)
@author: Administrator
"""
import tensorflow as tf
import random
import numpy as np
import DataManager as dm, Logger, CNN_model

log = Logger.get_logger("Model_1", "./log/Model_1.log")
weight_path = './weight/Model_1/Model_1.ckpt'

# tf Graph input
X = tf.compat.v1.placeholder("float", [None, 64 * 64])
Y = tf.compat.v1.placeholder("float", [None, 150])

#use convolutional neural network to extract features
feature_layer = CNN_model.CNN(X, Y)

#use fully connected neural network to classify
classification_layer = CNN_model.full_connected_layer(feature_layer)

# loss_softmax is the only loss function used in model 1
loss_softmax = tf.reduce_mean(
    tf.nn.softmax_cross_entropy_with_logits_v2(logits=classification_layer,
                                               labels=Y))
optimizer = tf.compat.v1.train.AdamOptimizer(learning_rate=0.001, epsilon=1)
train_op = optimizer.minimize(loss_softmax)

# TensorBoard
tf.compat.v1.summary.scalar("loss", loss_softmax)
merged_summary_op = tf.compat.v1.summary.merge_all()
if not os.path.exists(root_dir): os.makedirs(root_dir)

checkpoint_dir = os.path.join(root_dir, version +
                              '_train')  #os.path.dirname(checkpoint_path)
if not os.path.exists(checkpoint_dir): os.makedirs(checkpoint_dir)
checkpoint_path = os.path.join(checkpoint_dir, 'cp-{epoch:04d}.ckpt')

cp_callback = [
    tf.keras.callbacks.ModelCheckpoint(checkpoint_path,
                                       verbose=0,
                                       save_weights_only=True,
                                       period=5),
    tf.keras.callbacks.EarlyStopping(patience=5, monitor="val_acc")
]

model, model_archi = CNN_model.CNN(width, height, depth, len(genres))
model.save_weights(checkpoint_path.format(epoch=0))

history = model.fit(
    train_images,
    train_labels,
    epochs=epochs,
    callbacks=cp_callback,
    validation_data=(test_images, test_labels),
    #validation_split=0.2,
    shuffle=True,
    batch_size=batch_size,
    verbose=1)

#latest = tf.train.latest_checkpoint(checkpoint_dir)
# Save the weights