I'd like to ask how to print the cost value when there are two steps in model. Here is my code.
## SE_1st Hidden layer
W1 = tf.get_variable("W1", shape=[(2*spl+1)*feature_dim,layer_width_SE], initializer=tf.constant_initializer(value=W1_SE))
Variable = tf.get_variable("b1", shape=[layer_width_SE], initializer=tf.constant_initializer(value=b1_SE))
L11 = tf.nn.relu(tf.matmul(X, W1) + Variable)
L11 = tf.nn.dropout(L11, keep_prob=keep_prob)
## SE_2nd Hidden layer
W2 = tf.get_variable("W2", shape=[layer_width_SE,layer_width_SE], initializer=tf.constant_initializer(value=W2_SE))
Variable_1 = tf.get_variable("b2", shape=[layer_width_SE], initializer=tf.constant_initializer(value=b2_SE))
L12 = tf.nn.relu(tf.matmul(L11, W2)+ Variable_1)
L12 = tf.nn.dropout(L12, keep_prob=keep_prob)
## SE_3rd Hidden layer
W3 = tf.get_variable("W3", shape=[layer_width_SE, layer_width_SE], initializer=tf.constant_initializer(value=W3_SE))
Variable_2 = tf.get_variable("b3", shape=[layer_width_SE], initializer=tf.constant_initializer(value=b3_SE))
L13 = tf.nn.relu(tf.matmul(L12, W3) + Variable_2)
L13 = tf.nn.dropout(L13, keep_prob=keep_prob)
## SE_4th Hidden layer
W4 = tf.get_variable("W4", shape=[layer_width_SE,layer_width_SE], initializer=tf.constant_initializer(value=W4_SE))
Variable_3 = tf.get_variable("b4", shape=[layer_width_SE], initializer=tf.constant_initializer(value=b4_SE))
L14 = tf.nn.relu(tf.matmul(L13, W4)+ Variable_3)
L14 = tf.nn.dropout(L14, keep_prob=keep_prob)
## enhanced_speech_output layer
W5 = tf.get_variable("W5", shape=[layer_width_SE,feature_dim], initializer=tf.constant_initializer(value=W5_SE))
Variable_4 = tf.get_variable("b5", shape=[feature_dim], initializer=tf.constant_initializer(value=b5_SE))
SE_hypothesis = tf.matmul(L14, W5) + Variable_4
########################STOI DNN#########################
SE_hypothesis_append = tf.reshape(SE_hypothesis, [(batch_size_SE/frames), (feature_dim*frames)])
Y_append = tf.reshape(Y, [(batch_size_SE/frames), (feature_dim*frames)])
feature = tf.concat([SE_hypothesis_append, Y_append],axis=1)
## STOI_1st Hidden layer
W21 = tf.get_variable("W21", shape=[feature_dim*frames*2,layer_width_STOI], initializer=tf.constant_initializer(value=W1_STOI))
b21 = tf.get_variable("b21", shape=[layer_width_STOI], initializer=tf.constant_initializer(value=b1_STOI))
L21 = tf.nn.relu(tf.matmul(feature, W21) + b21)
L21 = tf.nn.dropout(L21, keep_prob=keep_prob)
## STOI_2nd Hidden layer
W22 = tf.get_variable("W22", shape=[layer_width_STOI,layer_width_STOI/2], initializer=tf.constant_initializer(value=W2_STOI))
b22 = tf.get_variable("b22", shape=[layer_width_STOI/2], initializer=tf.constant_initializer(value=b2_STOI))
L22 = tf.nn.relu(tf.matmul(L21, W22)+ b22)
L22 = tf.nn.dropout(L22, keep_prob=keep_prob)
## STOI_3rd Hidden layer
W23 = tf.get_variable("W23", shape=[layer_width_STOI/2,layer_width_STOI/4], initializer=tf.constant_initializer(value=W3_STOI))
b23 = tf.get_variable("b23", shape=[layer_width_STOI/4], initializer=tf.constant_initializer(value=b3_STOI))
L23 = tf.nn.relu(tf.matmul(L22, W23) + b23)
L23 = tf.nn.dropout(L23, keep_prob=keep_prob)
## STOI_4th Hidden layer
W24 = tf.get_variable("W24", shape=[layer_width_STOI/4,layer_width_STOI/8], initializer=tf.constant_initializer(value=W4_STOI))
b24 = tf.get_variable("b24", shape=[layer_width_STOI/8], initializer=tf.constant_initializer(value=b4_STOI))
L24 = tf.nn.relu(tf.matmul(L23, W24)+ b24)
L24 = tf.nn.dropout(L24, keep_prob=keep_prob)
## enhanced_speech_output layer
W25 = tf.get_variable("W25", shape=[layer_width_STOI/8,1], initializer=tf.constant_initializer(value=W5_STOI))
b25 = tf.get_variable("b25", shape=[1], initializer=tf.constant_initializer(value=b5_STOI))
STOI_hypothesis = tf.matmul(L24, W25) + b25
########################Cost function and optimizer#########################
SE_var_list = [W1, W2, W3, W4, W5, Variable, Variable_1, Variable_2, Variable_3, Variable_4]
cost_SE = tf.reduce_mean(tf.square(Y - SE_hypothesis))
cost_STOI = tf.reduce_mean(tf.square(STOI_target - STOI_hypothesis))
cost = (1-lamda)*cost_SE + lamda*cost_STOI
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost, var_list = SE_var_list)
saver = tf.train.Saver()
So what I want to know is the value of cost_SE and cost_STOI that I can set lamda for maximizing the efficiency of the model. I tried to do several ways but it doesn't worked.
feed_dict = {X: batch_con_x, Y: batch_con_y, STOI_target: STOI_maximum, keep_prob: 0.5}
c, _ = sess.run([cost, optimizer], feed_dict=feed_dict)
above code show only the sun of two cost value, but what I want to know is every cost value. Does it have any solution?
You can specify what you want in the first sess.run parameter:
c, cost_SE_eval, cost_STOI_eval, _ = sess.run(
[cost, cost_SE, cost_STOI, optimizer], feed_dict=feed_dict)
Related
I am looking at this error. I am making a part of the model untrainable, I get an error in the .fit.
would like to freeze the start of the model to only trade the head. top is the model, bottom is the .fit
def snakeEyes():
K = 100
i = tf.keras.layers.Input(shape=(7,11,17), name="matrix")
x1 = tf.keras.layers.Conv2D(K, 2,activation="relu",padding ="same")(i)
x1 = tf.keras.layers.Conv2D(K, 2,activation="relu")(x1)
x1 = tf.keras.layers.MaxPooling2D ()(x1)
x1 = tf.keras.layers.Conv2D(K, 2,activation="relu",padding ="same")(x1)
x1 = tf.keras.layers.Conv2D(K, 2,activation="relu")(x1)
x1 = tf.keras.layers.MaxPooling2D ()(x1)
x1 = tf.keras.layers.Flatten()(x1)
x2 = tf.keras.layers.Conv2D(K, 3,activation="relu",padding ="same")(i)
x2 = tf.keras.layers.Conv2D(K, 3,activation="relu",padding ="same")(x2)
x2 = tf.keras.layers.MaxPooling2D ()(x2)
x2 = tf.keras.layers.Conv2D(K, 3,activation="relu",padding ="same")(x2)
x2 = tf.keras.layers.Conv2D(K, 3,activation="relu",padding ="same")(x2)
x2 = tf.keras.layers.MaxPooling2D ()(x2)
x2 = tf.keras.layers.Flatten()(x2)
x = tf.keras.layers.concatenate([x1,x2])
x = tf.keras.layers.Dense(30,activation="relu")(x)
logits = tf.keras.layers.Dense(4)(x)
logits = tf.keras.layers.Softmax()(logits)
values = tf.keras.layers.Dense(64, activation='relu', name="out1")(x)
values = tf.keras.layers.Dense(1, activation="linear")(values)
actor = Model(inputs=i, outputs=logits)
actor.compile()
critic = Model(inputs=i, outputs=values)
critic.compile()
policy = Model(inputs=i, outputs=[logits,values])
policy.compile()
return actor,critic,policy
then in a loop :
for layer in agent1.critic.layers:layer.trainable = True
for layer in agent1.actor.layers:layer.trainable = False
agent1.critic.fit(state_matrices,rewards)
episode_reward = tf.math.reduce_sum(rewards)
return episode_reward , size
I get:
ValueError: No gradients provided for any variable: ['conv2d_104/kernel:0', 'conv2d_104/bias:0', ..
Your model is not compiled with any losses or optimizers. You have to define a loss and optimizer to be used and pass them to the compile. Something like:
optimizer = keras.optimizers.Adam(lr=learning_rate)
loss_fn = keras.losses.mean_squared_error()
actor.compile(optimizer=optimizer,
loss=loss_fn
I ran the code on this site in IDLE python https://sites.google.com/view/dailycoding00/main/%EC%95%8C%ED%8C%8C%EC%98%A4-%ED%95%99%EC%8A%B5
but it had an error
Traceback (most recent call last):
File "C:\Windows\system32\python", line 343, in <module>
ex = MyApp()
File "C:\Windows\system32\python", line 16, in __init__
self.initUI()
File "C:\Windows\system32\python", line 38, in initUI
self.board_cv2 = cv2.cvtColor(board_cv2, cv2.COLOR_BGR2RGB)
cv2.error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-xr4y3u3_\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
what does it mean?
code is
import sys
import numpy as np
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout
from PyQt5.QtGui import QPixmap, QImage
from PyQt5.QtCore import Qt
import cv2
import time
import tensorflow as tf
class MyApp(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
load_model_path = 'model/model.ckpt'
#
board_size = 15
self.game_end = 0
self.board_size = board_size
self.board = np.zeros([board_size,board_size])
self.board_history = np.zeros([board_size,board_size])
self.cnt = 1
time_now = time.gmtime(time.time())
self.save_name = str(time_now.tm_year) + '_' + str(time_now.tm_mon) + '_' + str(time_now.tm_mday) + '_' + str(time_now.tm_hour) + '_' + str(time_now.tm_min) + '_' + str(time_now.tm_sec) + '.txt'
self.save_name_png = str(time_now.tm_year) + '_' + str(time_now.tm_mon) + '_' + str(time_now.tm_mday) + '_' + str(time_now.tm_hour) + '_' + str(time_now.tm_min) + '_' + str(time_now.tm_sec) + '.png'
# read image in numpy array (using cv2)
board_cv2 = cv2.imread('c:\\omok_v1.0/source/board_1515.png')
self.board_cv2 = cv2.cvtColor(board_cv2, cv2.COLOR_BGR2RGB)
white_ball = cv2.imread('c:\\omok_v1.0/source/white.png')
self.white_ball = cv2.cvtColor(white_ball, cv2.COLOR_BGR2RGB)
black_ball = cv2.imread('c:\\omok_v1.0/source/black.png')
self.black_ball = cv2.cvtColor(black_ball, cv2.COLOR_BGR2RGB)
# numpy to QImage
height, width, channel = self.board_cv2.shape
bytesPerLine = 3 * width
qImg_board = QImage(self.board_cv2.data, width, height, bytesPerLine, QImage.Format_RGB888)
self.player = 1 # 1: 흑 / 2: 백
x = 0
y = 0
self.lbl_img = QLabel()
self.lbl_img.setPixmap(QPixmap(qImg_board))
self.vbox = QVBoxLayout()
self.vbox.addWidget(self.lbl_img)
self.setLayout(self.vbox)
# load AI model
self.X = tf.placeholder(tf.float32, [None, board_size*board_size])
X_img = tf.reshape(self.X, [-1, board_size, board_size, 1])
W1 = tf.Variable(tf.random_normal([7,7,1,64], stddev=0.1))
L1 = tf.nn.conv2d(X_img, W1, strides=[1,1,1,1], padding='SAME')
L1 = tf.nn.relu(L1)
#L1 = tf.nn.dropout(L1, keep_prob=keep_prob)
W2 = tf.Variable(tf.random_normal([5,5,64,32], stddev = 0.1))
L2 = tf.nn.conv2d(L1, W2, strides=[1,1,1,1], padding='SAME')
L2 = tf.nn.relu(L2)
#L2 = tf.nn.dropout(L2, keep_prob=keep_prob)
W3 = tf.Variable(tf.random_normal([3,3,32,32], stddev = 0.1))
L3 = tf.nn.conv2d(L2, W3, strides=[1,1,1,1], padding='SAME')
L3 = tf.nn.relu(L3)
#L3 = tf.nn.dropout(L3, keep_prob=keep_prob)
W4 = tf.Variable(tf.random_normal([3,3,32,32], stddev = 0.1))
L4 = tf.nn.conv2d(L3, W4, strides=[1,1,1,1], padding='SAME')
L4 = tf.nn.relu(L4)
#L4 = tf.nn.dropout(L4, keep_prob=keep_prob)
W5 = tf.Variable(tf.random_normal([3,3,32,32], stddev = 0.1))
L5 = tf.nn.conv2d(L4, W5, strides=[1,1,1,1], padding='SAME')
L5 = tf.nn.relu(L5)
#L5 = tf.nn.dropout(L5, keep_prob=keep_prob)
W6 = tf.Variable(tf.random_normal([3,3,32,32], stddev = 0.1))
L6 = tf.nn.conv2d(L5, W6, strides=[1,1,1,1], padding='SAME')
L6 = tf.nn.relu(L6)
W7 = tf.Variable(tf.random_normal([3,3,32,32], stddev = 0.1))
L7 = tf.nn.conv2d(L6, W7, strides=[1,1,1,1], padding='SAME')
L7 = tf.nn.relu(L7)
W8 = tf.Variable(tf.random_normal([3,3,32,32], stddev = 0.1))
L8 = tf.nn.conv2d(L7, W8, strides=[1,1,1,1], padding='SAME')
L8 = tf.nn.relu(L8)
W9 = tf.Variable(tf.random_normal([3,3,32,32], stddev = 0.1))
L9 = tf.nn.conv2d(L8, W9, strides=[1,1,1,1], padding='SAME')
L9 = tf.nn.relu(L9)
#L6 = tf.nn.dropout(L6, keep_prob=keep_prob)
L9 = tf.reshape(L9, [-1, board_size * board_size * 32])
W10 = tf.get_variable("W10", shape=[board_size * board_size * 32, board_size*board_size],initializer = tf.contrib.layers.xavier_initializer())
b10 = tf.Variable(tf.random_normal([board_size * board_size]))
self.logits =tf.matmul(L9,W10) + b10
self.saver = tf.train.Saver()
self.sess = tf.Session()
self.saver.restore(self.sess, load_model_path)
self.setWindowTitle('오목 시뮬레이션')
self.move(100, 100)
self.resize(500,500)
self.show()
def game_play(self, board_img, ball, pos_x, pos_y, turn):
#human
ball_size = ball.shape[0]
step_size = 56
off_set = 10
# ]
if pos_x < step_size/2+off_set+1 or pos_y < step_size/2+off_set+1:
print('그곳에는 둘 수 없습니다')
elif pos_x > step_size*self.board_size+step_size/2+off_set or pos_y > step_size*self.board_size+step_size/2+off_set:
print('그곳에는 둘 수 없습니다')
else:
step_x = round((pos_x - off_set)/step_size)
step_y = round((pos_y - off_set)/step_size)
if self.board[step_x-1,step_y-1] != 0: # 이미 돌이 있을때
print('그곳에는 둘 수 없습니다')
else:
self.board[step_x-1,step_y-1] = turn
self.board_history[step_x-1,step_y-1] = self.cnt
self.cnt = self.cnt + 1
x_step = step_size*step_x-round(step_size/2) + off_set
y_step = step_size*step_y-round(step_size/2) + off_set
board_img[x_step:x_step+ball_size,y_step:y_step+ball_size] = ball
#
if self.game_rule(self.board, turn):
self.game_end = 1
print('게임이 끝났습니다.')
board_img = cv2.cvtColor(board_img, cv2.COLOR_RGB2BGR)
print('축하합니다 당신이 승리 하였습니다')
return board_img
def sigmoid(self, x):
return 1 / (1 +np.exp(-x))
def find_max(self, result_mat):
max = 0
max_x = 0
max_y = 0
for i in range(self.board_size):
for j in range(self.board_size):
if result_mat[i,j] > max:
max = result_mat[i,j]
max_x = i
max_y = j
result_mat[max_x,max_y] = 0
return result_mat, max_x, max_y
def mousePressEvent(self, e):
x = e.x()
y = e.y()
if self.game_end == 0:
#
self.board_cv2 = self.game_play(self.board_cv2, self.black_ball, y, x, 1)
save_name = 'result/' + str(self.cnt) + "board_black.png"
save_name_w = 'result/' + str(self.cnt) + "board_white.png"
save_name_pred = 'result/' + str(self.cnt) + "board_pred.png"
#
input_X = self.board.flatten()/2
result = self.sess.run(self.logits, feed_dict={self.X: input_X[None,:]})
result_mat = self.sigmoid(result).reshape([self.board_size,self.board_size])
heat_map = cv2.resize(result_mat*255, (500, 500))
result_mat, max_x, max_y = self.find_max(result_mat)
save_image = cv2.resize(self.board_cv2, (500, 500), interpolation=cv2.INTER_CUBIC)
save_image = cv2.cvtColor(save_image, cv2.COLOR_RGB2BGR)
#save_image[:,:,0] = save_image[:,:,0] + heat_map
cv2.imwrite(save_name, save_image)
cv2.imwrite(save_name_pred, heat_map)
#
while self.board[max_x,max_y] !=0:
print('AI 거긴 둘 수 없다')
result_mat, max_x, max_y = self.find_max(result_mat)
self.board[max_x,max_y] = 2 # 인공지능은 항상 2값 / 사람은 1값으로 표현
self.board_history[max_x,max_y] = self.cnt
self.cnt = self.cnt + 1
ball_size = self.white_ball.shape[0]
step_size = 56
off_set = 10
x_step = step_size*(max_x+1)-round(step_size/2) + off_set
y_step = step_size*(max_y+1)-round(step_size/2) + off_set
self.board_cv2[x_step:x_step+ball_size,y_step:y_step+ball_size] = self.white_ball
save_image = cv2.resize(self.board_cv2, (500, 500), interpolation=cv2.INTER_CUBIC)
save_image = cv2.cvtColor(save_image, cv2.COLOR_RGB2BGR)
cv2.imwrite(save_name_w, save_image)
#self.board_history[step_x-1,step_y-1] = self.cnt
#self.board_cv2 = self.game_play(self.board_cv2, self.white_ball, y, x, 2)
height, width, channel = self.board_cv2.shape
bytesPerLine = 3 * width
qImg_board = QImage(self.board_cv2.data, width, height, bytesPerLine, QImage.Format_RGB888)
self.lbl_img.setPixmap(QPixmap(qImg_board))
def game_rule(self, board, player): # 추후 오목 국룰 (렌주룰) 도입 예정
game_result = 0
diag_line = np.zeros(5)
# ●●●●●
for i_idx in range(len(board)):
for j_idx in range(len(board)-4):
p1 = (board[i_idx,j_idx:j_idx+5] == player)
if p1.sum() == 5:
#print('player ', player, ' win')
game_result = 1
return game_result
#print(board)
for i_idx in range(len(board)-4):
for j_idx in range(len(board)):
p1 = (board[i_idx:i_idx+5,j_idx] ==player)
if p1.sum() == 5:
#print('player ', player, ' win')
game_result = 1
return game_result
#print(board)
#
for i_idx in range(len(board)-4):
for j_idx in range(len(board)-4):
diag_line[0] = board[i_idx+0,j_idx+0]
diag_line[1] = board[i_idx+1,j_idx+1]
diag_line[2] = board[i_idx+2,j_idx+2]
diag_line[3] = board[i_idx+3,j_idx+3]
diag_line[4] = board[i_idx+4,j_idx+4]
p1 = (diag_line == player)
if p1.sum() == 5:
#print('player ', player, ' win')
game_result = 1
return game_result
#print(board)
#
for i_idx in range(len(board)-4):
for j_idx in range(len(board)-4):
diag_line[0] = board[i_idx+4,j_idx+0]
diag_line[1] = board[i_idx+3,j_idx+1]
diag_line[2] = board[i_idx+2,j_idx+2]
diag_line[3] = board[i_idx+1,j_idx+3]
diag_line[4] = board[i_idx+0,j_idx+4]
p1 = (diag_line == player)
if p1.sum() == 5:
game_result = 1
return game_result
return game_result
def save_history(self):
result=np.array(self.board_history).flatten()
f = open(self.save_name, 'w')
f.write(np.array2string(result))
f.close()
if name == 'main':
app = QApplication(sys.argv)
ex = MyApp()
sys.exit(app.exec_())
I am trying to use the Triple-Loss technique to fine-tune an EfficientNet network for human Re-ID using Keras. Here is the code I am using:
This is the generator:
class SampleGen(object):
def __init__(self, file_class_mapping):
self.file_class_mapping = file_class_mapping
self.class_to_list_files = defaultdict(list)
self.list_all_files = list(file_class_mapping.keys())
self.range_all_files = list(range(len(self.list_all_files)))
for file, class_ in file_class_mapping.items():
self.class_to_list_files[class_].append(file)
self.list_classes = list(set(self.file_class_mapping.values()))
self.range_list_classes = range(len(self.list_classes))
self.class_weight = np.array([len(self.class_to_list_files[class_]) for class_ in self.list_classes])
self.class_weight = self.class_weight / np.sum(self.class_weight)
def get_sample(self):
class_idx = np.random.choice(self.range_list_classes, 1, p=self.class_weight)[0]
examples_class_idx = np.random.choice(range(len(self.class_to_list_files[self.list_classes[class_idx]])), 2)
positive_example_1, positive_example_2 = \
self.class_to_list_files[self.list_classes[class_idx]][examples_class_idx[0]], \
self.class_to_list_files[self.list_classes[class_idx]][examples_class_idx[1]]
negative_example = None
while negative_example is None or self.file_class_mapping[negative_example] == \
self.file_class_mapping[positive_example_1]:
negative_example_idx = np.random.choice(self.range_all_files, 1)[0]
negative_example = self.list_all_files[negative_example_idx]
return positive_example_1, negative_example, positive_example_2
def read_and_resize(filepath):
im = Image.open((filepath)).convert('RGB')
im = im.resize((image_size, image_size))
return np.array(im, dtype="float32")
def augment(im_array):
if np.random.uniform(0, 1) > 0.9:
im_array = np.fliplr(im_array)
return im_array
def gen(triplet_gen):
while True:
list_positive_examples_1 = []
list_negative_examples = []
list_positive_examples_2 = []
for i in range(batch_size):
positive_example_1, negative_example, positive_example_2 = triplet_gen.get_sample()
path_pos1 = join(path_train, positive_example_1)
path_neg = join(path_train, negative_example)
path_pos2 = join(path_train, positive_example_2)
positive_example_1_img = read_and_resize(path_pos1)
negative_example_img = read_and_resize(path_neg)
positive_example_2_img = read_and_resize(path_pos2)
positive_example_1_img = augment(positive_example_1_img)
negative_example_img = augment(negative_example_img)
positive_example_2_img = augment(positive_example_2_img)
list_positive_examples_1.append(positive_example_1_img)
list_negative_examples.append(negative_example_img)
list_positive_examples_2.append(positive_example_2_img)
A = preprocess_input(np.array(list_positive_examples_1))
B = preprocess_input(np.array(list_positive_examples_2))
C = preprocess_input(np.array(list_negative_examples))
label = None
yield {'anchor_input': A, 'positive_input': B, 'negative_input': C}, label
This is how I create the model:
def get_model():
base_model = efn.EfficientNetB3(weights='imagenet', include_top=False)
for layer in base_model.layers:
layer.trainable = False
x = base_model.output
x = Dropout(0.6)(x)
x = Dense(embedding_dim)(x)
x = Lambda(lambda x: K.l2_normalize(x, axis=1), name="enc_out")(x)
embedding_model = Model(base_model.input, x, name="embedding")
input_shape = (image_size, image_size, 3)
anchor_input = Input(input_shape, name='anchor_input')
positive_input = Input(input_shape, name='positive_input')
negative_input = Input(input_shape, name='negative_input')
anchor_embedding = embedding_model(anchor_input)
positive_embedding = embedding_model(positive_input)
negative_embedding = embedding_model(negative_input)
inputs = [anchor_input, positive_input, negative_input]
outputs = [anchor_embedding, positive_embedding, negative_embedding]
triplet_model = Model(inputs, outputs)
triplet_model.add_loss(K.mean(triplet_loss(outputs)))
return embedding_model, triplet_model
And this is how I'm trying to run the training:
if __name__ == '__main__':
data = pd.read_csv(path_csv)
train, test = train_test_split(data, train_size=0.7, random_state=1337)
file_id_mapping_train = {k: v for k, v in zip(train.Image.values, train.Id.values)}
file_id_mapping_test = {k: v for k, v in zip(test.Image.values, test.Id.values)}
gen_tr = gen(SampleGen(file_id_mapping_train))
gen_te = gen(SampleGen(file_id_mapping_test))
embedding_model, triplet_model = get_model()
for i, layer in enumerate(embedding_model.layers):
print(i, layer.name, layer.trainable)
for layer in embedding_model.layers[379:]:
layer.trainable = True
for layer in embedding_model.layers[:379]:
layer.trainable = False
triplet_model.compile(loss=None, optimizer=Adam(0.0001))
history = triplet_model.fit(x=gen_tr,
validation_data=gen_te,
epochs=10,
verbose=1,
steps_per_epoch=200,
validation_steps=20,
callbacks=create_callbacks())
The csv contains two columns (Image, Id) and I am generating triplets on the go using a generator. The layer 379 is the last layer of the network so I just leave that as trainable. I let it run for some epochs and it seems like it doesn't converge, it stays around 2.30. On epochs like 20, the loss is even higher than what I've started with. Here you can see what I mean: train example Is there anything wrong with the way I think about the problem?
Thank you!
I am implementing tensorflow version of LBCNN which has the code here: https://github.com/juefeix/lbcnn.torch. The problem here is that when I try to rewrite in tensorflow, the cost function keeps high and fluctuating. It mires me 2 weeks that even I debug everything, I still don't know where am I wrong.
Torch code:
-- resnet.lua
local function createModel(opt)
local function basicblock(nChIn,nChOut,sz)
local s = nn.Sequential()
local shareConv = Convolution(nChIn,nChOut,sz,sz,1,
1,(sz-1)/2,(sz-1)/2)
s:add(SBatchNorm(nChIn))
s:add(shareConv)
s:add(ReLU())
s:add(Convolution(nChOut,nChIn,1,1))
local identity = nn.Identity()
local output= nn.Sequential(): add(nn.ConcatTable()
:add(s):add(identity)):add(nn.CAddTable(true))
return output
end
local sz = opt.convSize
local nInputPlane = opt.nInputPlane
local nChIn = opt.numChannels
local nChOut = opt.numWeights
-- define model to train
model = nn.Sequential()
model:add(Convolution(nInputPlane,nChIn,sz,sz,1,1,1,1))
model:add(SBatchNorm(nChIn))
model:add(ReLU(true))
for stages = 1,opt.depth do
model:add(basicblock(nChIn,nChOut,sz))
end
model:add(Avg(5,5,5,5))
-- stage 3 : standard 2-layer neural network
model:add(nn.Reshape(nChIn*opt.view))
model:add(nn.Dropout(0.5))
model:add(nn.Linear(nChIn*opt.view,
math.max(opt.nClasses,opt.full)))
model:add(cudnn.ReLU())
model:add(nn.Dropout(0.5))
model: add(nn.Linear (math.max(opt.full,opt.nClasses), opt.nClasses))
model:cuda()
return model
end
return createModel
Tensorflow code:
def cnn(prev_input, lbc_size, lbc_channels, output_channels):
shortcut = tf.identity(prev_input)
B = tf.contrib.layers.batch_norm(prev_input)
Z = tf.contrib.layers.conv2d(inputs = B, num_outputs = lbc_channels,
kernel_size = 3, stride = 1, padding = "SAME", activation_fn = None)
A1 = tf.nn.relu(Z)
A2 = tf.contrib.layers.conv2d(inputs = A1,
num_outputs=output_channels,
kernel_size = 1, stride = 1,
padding = "SAME", activation_fn = None)
A3 = tf.add(A2, shortcut)
return A3
def model(X, Keep_probability):
with tf.name_scope("Pre-Conv"):
X1 = tf.contrib.layers.conv2d(inputs = X, num_outputs =
output_channels,kernel_size = lbc_size, stride = 1,
padding = "SAME", activation_fn = None)
X2 = tf.contrib.layers.batch_norm(X1)
X3 = tf.nn.relu(X2)
X_in = X3
for i in range(conv_layers):
with tf.name_scope("conv"):
X_out,BB,ZZ,AA,AAA = cnn(X_in, lbc_size, lbc_channels,
out_channels)
X_in = X_out
with tf.name_scope("AvgPool"):
Z = tf.nn.avg_pool(value = X_in, ksize = [1, 5, 5, 1],
strides = [1, 5, 5, 1], padding = "VALID")
with tf.name_scope("Flatter"):
P = tf.contrib.layers.flatten(Z)
with tf.name_scope("Dropout"):
F1 = tf.nn.dropout(x = P, keep_prob = 0.5)
with tf.name_scope("Fully"):
F2 = tf.contrib.layers.fully_connected(inputs = F1,
num_outputs = fc_hidden_units, activation_fn = tf.nn.relu)
with tf.name_scope("Dropout"):
F3 = tf.nn.dropout(x = F2, keep_prob = 0.5)
with tf.name_scope("Fully"):
F4 = tf.contrib.layers.fully_connected(inputs = F3,
num_outputs = output_classes, activation_fn = None)
return F4
Assume all the parameters, I passed correctly. I just want to ask that whether 2 architecture is the same or not? One more thing is their code using SGD with momentum and weight_decay while I use AdamOptimizer, is this make the difference? Thank you so much.
I did not look over your entire code to check that indeed it's the same, but usually there is a difference in examples to convergence as you change the optimizer from momentum to adam. You should also need to retune hyperparameters to get good performance.
I am a beginner in tensorflow and I am trying to train a model using "mini batch". To do that I created a generator and iterate it. The problem I encounter is that, at the beginning of the epoch, the train seems fast (many batch per seconds) then the train slow down (1 batch per second) so I am wondering where I am wrong in my code but I do not find the problem.
def prepare_data(filename):
'''load file which give path and label for the data'''
f = open(filename, 'r')
data = [line.split() for line in f]
feat =[]
label=[]
for l in data:
feat.append(l[0])
label.append(l[1])
n_samples = len(feat)
shuf = list(range(n_samples))
random.shuffle(shuf)
count = Counter(label)
print(count)
feature = [feat[i] for i in shuf]
label = np.array(label, dtype=np.int)
return feature, label[shuf]
def get_specgrams(paths, nsamples=16000):
'''
Given list of paths, return specgrams.
'''
# read the wav files
wavs = [wavfile.read(x)[1] for x in paths]
# zero pad the shorter samples and cut off the long ones.
data = []
for wav in wavs:
if wav.size < 16000:
d = np.pad(wav, (nsamples - wav.size, 0), mode='constant')
else:
d = wav[0:nsamples]
data.append(d)
# get the specgram
#specgram = [signal.spectrogram(d, nperseg=256, noverlap=128)[2] for d in data]
#specgram = [s.reshape(129, 124, -1) for s in specgram]
return np.asarray(data)
def get_specgram(path, nsamples=16000):
'''
Given path, return specgrams.
'''
# read the wav files
wav = wavfile.read(path)[1]
# zero pad the shorter samples and cut off the long ones.
if wav.size < 16000:
d = np.pad(wav, (nsamples - wav.size, 0), mode='constant')
else:
d = wav[0:nsamples]
# get the specgram
#specgram = [signal.spectrogram(d, nperseg=256, noverlap=128)[2] for d in data]
#specgram = [s.reshape(129, 124, -1) for s in specgram]
return d
# multci classification binary labels
def one_hot_encode(labels, n_unique_labels=31):
n_labels = len(labels)
#print('number of unique labels:', n_unique_labels)
one_hot_encode = np.zeros((n_labels,n_unique_labels))
one_hot_encode[np.arange(n_labels), labels] = 1
return np.array(one_hot_encode, dtype=np.int)
#create_path_file('train/audio/')
def model(tr_features, tr_labels, ts_features, ts_labels):
# remove gpu device error
config = tf.ConfigProto(allow_soft_placement = True)
# parameters
BATCH_SIZE = 4
number_loop = math.ceil(len(tr_features)/BATCH_SIZE)
training_epochs = 10
n_dim = 16000
n_classes = 31 #len(np.unique(ts_labels))
n_hidden_units_one = 280
n_hidden_units_two = 300
sd = 1 / np.sqrt(n_dim)
learning_rate = 0.1
# get test data
ts_features, ts_labels = get_data(ts_features, ts_labels)
# Model
X = tf.placeholder(tf.float32,[None,n_dim])
Y = tf.placeholder(tf.float32,[None,n_classes])
W_1 = tf.Variable(tf.random_normal([n_dim,n_hidden_units_one], mean = 0, stddev=sd))
b_1 = tf.Variable(tf.random_normal([n_hidden_units_one], mean = 0, stddev=sd))
h_1 = tf.nn.tanh(tf.matmul(X,W_1) + b_1)
W_2 = tf.Variable(tf.random_normal([n_hidden_units_one,n_hidden_units_two], mean = 0, stddev=sd))
b_2 = tf.Variable(tf.random_normal([n_hidden_units_two], mean = 0, stddev=sd))
h_2 = tf.nn.sigmoid(tf.matmul(h_1,W_2) + b_2)
W = tf.Variable(tf.random_normal([n_hidden_units_two,n_classes], mean = 0, stddev=sd))
b = tf.Variable(tf.random_normal([n_classes], mean = 0, stddev=sd))
y_ = tf.nn.softmax(tf.matmul(h_2,W) + b)
init = tf.initialize_all_variables()
# function and optimizers
cost_function = -tf.reduce_sum(Y * tf.log(y_))
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost_function)
correct_prediction = tf.equal(tf.argmax(y_,1), tf.argmax(Y,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
# train loop
cost_history = np.empty(shape=[1],dtype=float)
y_true, y_pred = None, None
with tf.Session(config = config) as sess:
sess.run(init)
for epoch in range(training_epochs):
print(' ## Epoch n°', epoch+1 )
batch = batch_generator(BATCH_SIZE, tr_features, tr_labels)
acc_total = 0.0
for cpt, (train_features_batch, train_labels_batch) in enumerate(batch):
_,cost = sess.run([optimizer,cost_function],feed_dict={X:train_features_batch,Y:train_labels_batch})
cost_history = np.append(cost_history,cost)
correct_prediction = tf.equal(tf.argmax(y_,1), tf.argmax(Y,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
acc = accuracy.eval(feed_dict={X:train_features_batch,Y:train_labels_batch})
acc_total = (acc_total*cpt + acc)/(cpt+1)
print('Train accuracy : ', acc_total, '[',str(cpt+1), '/',str(number_loop), ']' ,flush=True, end='\r')
clear_output()
print('Train accuracy : ', acc_total)
y_pred = sess.run(tf.argmax(y_,1),feed_dict={X: ts_features})
y_true = sess.run(tf.argmax(ts_labels,1))
print('Test accuracy: ', round(sess.run(accuracy, feed_dict={X: ts_features, Y: ts_labels}) , 3))
fig = plt.figure(figsize=(10,8))
plt.plot(cost_history)
plt.axis([0,training_epochs,0,np.max(cost_history)])
plt.show()
p,r,f,s = precision_recall_fscore_support(y_true, y_pred, average='micro')
print("F-Score:", round(f,3))
def batch_generator(batch_size, feat_path, labels):
n_sample = len(feat_path)
ite = math.ceil(n_sample/batch_size)
for i in range(0, ite):
if i == ite-1:
label = one_hot_encode(labels[-batch_size:])
feat = get_specgrams(feat_path[-batch_size:])
yield (feat, label)
else:
label = one_hot_encode(labels[i*batch_size:i*batch_size+batch_size])
feat = get_specgrams(feat_path[i*batch_size:i*batch_size+batch_size])
yield (feat, label)
def get_data(feat_path, labels):
feat = get_specgrams(feat_path)
label = one_hot_encode(labels)
return feat, label
def __main__():
print('## Load data and shuffle')
feat_path, labels = prepare_data('data_labelised2.txt')
idx = int(len(labels)*0.8)
print("## Create Model")
model(feat_path[0:idx], labels[0:idx], feat_path[idx+1:], labels[idx+1:])
with tf.device('/gpu:0'):
__main__()