def build_network(self): # 32 layers: n=5, 56 layers: n=9, 110 layers: n=18 n = 5 #https://github.com/tflearn/tflearn/blob/master/examples/images/residual_network_cifar10.py print('[+] Building RESIDUAL NETWORK') print ('[-] COLOR: ' + str(COLOR)) print('[-] BATH_SIZE' + str(BATH_SIZE_CONSTANT)) print('[-] EXPERIMENTAL_LABEL' + EXPERIMENTO_LABEL) self.network = input_data(shape=[None, SIZE_FACE, SIZE_FACE, COLOR]) self.network = tflearn.conv_2d(self.network, 16, 3, regularizer='L2', weight_decay=0.0001) self.network = tflearn.residual_block(self.network, n, 16) self.network = tflearn.residual_block(self.network, 1, 32, downsample=True) self.network = tflearn.residual_block(self.network, n-1, 32) self.network = tflearn.residual_block(self.network, 1, 64, downsample=True) self.network = tflearn.residual_block(self.network, n-1, 64) self.network = tflearn.batch_normalization(self.network) self.network = tflearn.activation(self.network, 'relu') self.network = tflearn.global_avg_pool(self.network) # Regression self.network = tflearn.fully_connected(self.network, len(EMOTIONS), activation='softmax') self.mom = tflearn.Momentum(0.1, lr_decay=0.1, decay_step=32000, staircase=True) self.network = tflearn.regression(self.network, optimizer=self.mom, loss='categorical_crossentropy') self.model = tflearn.DNN( self.network, checkpoint_path = CHECKPOINT_DIR, max_checkpoints = 1, tensorboard_dir = TENSORBOARD_DIR, #best_checkpoint_path = CHECKPOINT_DIR_BEST, tensorboard_verbose = 1 ) self.load_model()
def resnet(X, y): y = tf.one_hot(y, 10) network = tflearn.conv_2d(X, 16, 3, regularizer="L2", weight_decay=0.0001) network = tflearn.residual_block(network, 5, 16) network = tflearn.residual_block(network, 1, 32, downsample=True) network = tflearn.residual_block(network, 5 - 1, 32) network = tflearn.residual_block(network, 1, 64, downsample=True) network = tflearn.residual_block(network, 5 - 1, 64) network = tflearn.batch_normalization(network) network = tflearn.activation(network, "relu") network = tflearn.global_avg_pool(network) logits = tflearn.fully_connected(network, 10, activation="softmax") loss = tflearn.categorical_crossentropy(logits, y) step_tensor = tf.Variable(0., name="Training_step", trainable=False) optimizer = tflearn.Momentum(0.1, lr_decay=0.1, decay_step=32000, staircase=True) optimizer.build(step_tensor) train_op = optimizer().minimize(loss) return logits, loss, train_op
def conv_res_integrated(img_size, lr, n, img_aug): tf.reset_default_graph() convnet = input_data(shape=[None, img_size, img_size, 1], name='input', data_augmentation=img_aug) # conv layer 1 w/max pooling conv1 = conv_2d(convnet, 32, 2, activation='relu', regularizer='L2', weight_decay=0.0001) conv1 = max_pool_2d(conv1, 2) # conv layer 2 w/max pooling etc conv2 = conv_2d(conv1, 32, 2, activation='relu', regularizer='L2', weight_decay=0.0001) conv2 = max_pool_2d(conv2, 2) conv3 = conv_2d(conv2, 64, 2, activation='relu', regularizer='L2', weight_decay=0.0001) conv3 = max_pool_2d(conv3, 2) conv4 = conv_2d(conv3, 64, 2, activation='relu', regularizer='L2', weight_decay=0.0001) conv4 = max_pool_2d(conv4, 2) # residual block res1 = residual_block(conv4, n, 128, downsample=True, regularizer='L2', weight_decay=0.0001) batch_norm = batch_normalization(res1) activ = tflearn.activation(batch_norm, 'relu') gap = tflearn.global_avg_pool(activ) # fully connected layer 1 fc1 = fully_connected(gap, 1024, activation='relu', regularizer='L2', weight_decay=0.0001) fc1 = dropout(fc1, 0.85) # fully connected layer 2 fc2 = tflearn.fully_connected(fc1, 2, activation='softmax') # output layer mom = tflearn.Momentum(0.1, lr_decay=0.01, decay_step=32000, staircase=True) output = tflearn.regression(fc2, optimizer=mom, learning_rate=lr, loss='categorical_crossentropy') # Training model = tflearn.DNN(output, checkpoint_path='model_integrated', max_checkpoints=2, tensorboard_verbose=0, tensorboard_dir='log', clip_gradients=0.) return model
def res_graph(X): img_prep = ImagePreprocessing() img_prep.add_featurewise_zero_center() img_prep.add_featurewise_stdnorm() img_aug = ImageAugmentation() img_aug.add_random_flip_leftright() img_aug.add_random_rotation(max_angle=25.) img_aug.add_random_crop([64, 64], padding=4) n = 5 net = input_data(shape=[None, 64, 64, 3], data_preprocessing=img_prep, data_augmentation=img_aug) net = tflearn.conv_2d(net, 16, 3, regularizer='L2', weight_decay=0.0001) net = tflearn.residual_block(net, n, 16) net = tflearn.residual_block(net, 1, 32, downsample=True) net = tflearn.residual_block(net, n - 1, 32) net = tflearn.residual_block(net, 1, 64, downsample=True) net = tflearn.residual_block(net, n - 1, 64) net = tflearn.batch_normalization(net) net = tflearn.activation(net, 'relu') net = tflearn.global_avg_pool(net) net = tflearn.fully_connected(net, 2, activation='softmax') mom = tflearn.Momentum(0.1, lr_decay=0.1, decay_step=32000, staircase=True) net = tflearn.regression(net, optimizer=mom, loss='categorical_crossentropy') model = tflearn.DNN(net) # rnn typo -> res model.load('model\\res\\jun_rnn_cat_dog.tflearn') res_result = model.predict(X) return res_result
def create_dual_network(self, inputs, s_dim): with tf.variable_scope(self.scope + '-dual', reuse=self.reuse): split_array = [] for i in range(s_dim[0]): tmp = tf.reshape(inputs[:, i:i + 1, :], (-1, s_dim[1], 1)) branch1 = tflearn.conv_1d(tmp, FEATURE_NUM, 3, activation='relu') branch2 = tflearn.conv_1d(tmp, FEATURE_NUM, 4, activation='relu') branch3 = tflearn.conv_1d(tmp, FEATURE_NUM, 5, activation='relu') network = tflearn.merge([branch1, branch2, branch3], mode='concat', axis=1) network = tf.expand_dims(network, 2) network = tflearn.global_avg_pool(network) split_array.append(network) #out, _ = self.attention(split_array, FEATURE_NUM) out = tflearn.merge(split_array, 'concat') self.reuse = True return out
def create_Resnet(num_classes): # Residual blocks # 32 layers: n=5, 56 layers: n=9, 110 layers: n=18 n = 9 network = tflearn.input_data( shape=[None, 64, 64, 3]) #, data_preprocessing=img_prep, data_augmentation=img_aug) network = tflearn.conv_2d(network, 16, 3, regularizer='L2', weight_decay=0.0001) network = tflearn.residual_block(network, n, 16) network = tflearn.residual_block(network, 1, 32, downsample=True) network = tflearn.residual_block(network, n - 1, 32) network = tflearn.residual_block(network, 1, 64, downsample=True) network = tflearn.residual_block(network, n - 1, 64) network = tflearn.batch_normalization(network) network = tflearn.activation(network, 'relu') network = tflearn.global_avg_pool(network) # Regression network = tflearn.fully_connected(network, num_classes, activation='softmax') mom = tflearn.Momentum(0.1, lr_decay=0.1, decay_step=32000, staircase=True) network = tflearn.regression(network, optimizer=mom, loss='categorical_crossentropy') return network
def ResNet32(tensorWidth, tensorHeight, tensorDepth, tb_verb=0): n = 5 net = tflearn.input_data( shape=[None, tensorWidth, tensorHeight, tensorDepth]) net = tflearn.conv_2d(net, 16, 3, regularizer='L2', weight_decay=0.0001) net = tflearn.residual_block(net, n, 16) net = tflearn.residual_block(net, 1, 32, downsample=True) net = tflearn.residual_block(net, n - 1, 32) net = tflearn.residual_block(net, 1, 64, downsample=True) net = tflearn.residual_block(net, n - 1, 64) net = tflearn.batch_normalization(net) net = tflearn.activation(net, 'relu') net = tflearn.global_avg_pool(net) # Regression net = tflearn.fully_connected(net, 2, activation='softmax') mom = tflearn.Momentum(0.1, lr_decay=0.1, decay_step=32000, staircase=True) net = tflearn.regression(net, optimizer=mom, loss='categorical_crossentropy') # Training model = tflearn.DNN(net, checkpoint_path='model_resnet_bee1', max_checkpoints=10, tensorboard_verbose=tb_verb, clip_gradients=0.) return model
def build_network(self): """ Build the convnet. Input is 48x48 3072 nodes in fully connected layer """ # Real-time data preprocessing img_prep = tflearn.ImagePreprocessing() img_prep.add_featurewise_zero_center( per_channel=True, mean=[0.53990436, 0.4405486, 0.39328504]) # Real-time data augmentation img_aug = tflearn.ImageAugmentation() img_aug.add_random_flip_leftright() img_aug.add_random_crop([49, 49], padding=4) # Building Residual Network self.network = tflearn.input_data(shape=[None, 49, 49, 3], data_preprocessing=img_prep, data_augmentation=img_aug) self.network = tflearn.conv_2d(self.network, 16, 3, regularizer='L2', weight_decay=0.0001) self.network = tflearn.resnext_block(self.network, 5, 16, 32) self.network = tflearn.resnext_block(self.network, 1, 32, 32, downsample=True) self.network = tflearn.resnext_block(self.network, 4, 32, 32) self.network = tflearn.resnext_block(self.network, 1, 64, 32, downsample=True) self.network = tflearn.resnext_block(self.network, 4, 64, 32) self.network = tflearn.batch_normalization(self.network) self.network = tflearn.activation(self.network, 'relu') self.network = tflearn.global_avg_pool(self.network) # Regression self.network = tflearn.fully_connected(self.network, 11, activation='softmax') opt = tflearn.Momentum(0.1, lr_decay=0.1, decay_step=32000, staircase=True) self.network = tflearn.regression(self.network, optimizer=opt, loss='categorical_crossentropy') # Training self.model = tflearn.DNN(self.network, checkpoint_path='Snapshots/model_resnext', max_checkpoints=10, tensorboard_verbose=0, tensorboard_dir='Logs/', clip_gradients=0.) self.load_model()
def resnet(): # residual blocks: 32 layers: n=5, 56 layers: n=9, 110 layers: n=18 n = 5 # building Residual Network network = tflearn.input_data(shape=[None, 128, 128, 3]) network = tflearn.conv_2d(network, 16, 3, regularizer='L2', weight_decay=0.0001) network = tflearn.residual_block(network, n, 16) network = tflearn.residual_block(network, 1, 32, downsample=True) network = tflearn.residual_block(network, n - 1, 32) network = tflearn.residual_block(network, 1, 64, downsample=True) network = tflearn.residual_block(network, n - 1, 64) network = tflearn.batch_normalization(network) network = tflearn.activation(network, 'relu') network = tflearn.global_avg_pool(network) # regression network = tflearn.fully_connected(network, 2, activation='softmax') network = tflearn.regression(network, optimizer='adam', loss='categorical_crossentropy') return network
def ResNet26(network, num_out, drop_prob=1.0): n = 2 # number of residual blocks per layer network = tflearn.conv_2d(network, 32, 7, regularizer='L2', strides=2, activation='relu') network = max_pool_2d(network, 3, strides=2) network = tflearn.residual_block(network, n, 32, activation='relu') network = tflearn.residual_block(network, n, 32, activation='relu') network = tflearn.residual_block(network, n, 64, downsample=True, activation='relu') network = tflearn.residual_block(network, n, 64, activation='relu') network = tflearn.residual_block(network, n, 128, activation='relu') network = tflearn.residual_block(network, n, 128, activation='relu') network = batch_normalization(network) network = activation(network, 'relu') network = global_avg_pool(network) network = tflearn.fully_connected(network, num_out, activation='softmax') return network
def DenseNet(network): # Growth Rate (12, 16, 32, ...) k = 3 # Depth (40, 100, ...) L = 28 nb_layers = int((L - 4) / 3) # Building DenseNet Network network = tflearn.conv_2d(network, 10, 4, regularizer='L2', weight_decay=0.0001) network = denseblock(network, nb_layers, k, dropout=drop_prob) network = denseblock(network, nb_layers, k, dropout=drop_prob) network = denseblock(network, nb_layers, k, dropout=drop_prob) network = tflearn.global_avg_pool(network) # Regression network = tflearn.fully_connected(network, 4, activation='softmax') return network
def buildNetwork(n, k): # Real-time data preprocessing img_prep = tflearn.ImagePreprocessing() img_prep.add_featurewise_zero_center(per_channel=True) img_prep.add_featurewise_stdnorm() # Real-time data augmentation img_aug = tflearn.ImageAugmentation() img_aug.add_random_flip_leftright() # Building Residual Network net = tflearn.input_data(shape=[None, b.IMG_WIDTH, b.IMG_HEIGHT, b.CHANNELS], data_preprocessing=img_prep, data_augmentation=img_aug) net = tflearn.conv_2d(net, 16, 3, regularizer='L2', weight_decay=0.0001) # wideresnet part net = wideresnet_block(net, n, 16, k, downsample=True) net = wideresnet_block(net, 1, 32, k, downsample=True) net = wideresnet_block(net, n - 1, 32, k, downsample=True) net = wideresnet_block(net, 1, 64, 2, downsample=True) net = wideresnet_block(net, n - 1, 64, k, downsample=True) net = tflearn.batch_normalization(net) net = tflearn.activation(net, 'relu') net = tflearn.global_avg_pool(net) # Regression net = tflearn.fully_connected(net, b.CLASS_3_NUMBER, activation='softmax') mom = tflearn.Momentum(0.1, lr_decay=0.1, decay_step=32000, staircase=True) net = tflearn.regression(net, optimizer=mom, loss='categorical_crossentropy') model = tflearn.DNN(net, tensorboard_verbose=0, clip_gradients=0., tensorboard_dir=b.PATH + 'log') return model
def _model2(): global yTest, img_aug tf.reset_default_graph() img_prep = ImagePreprocessing() img_prep.add_featurewise_zero_center() img_prep.add_featurewise_stdnorm() net = input_data(shape=[None, inputSize, inputSize, dim], name='input', data_preprocessing=img_prep, data_augmentation=img_aug) n = 2 j = 64 ''' net = tflearn.conv_2d(net, j, 3, regularizer='L2', weight_decay=0.0001) net = tflearn.residual_block(net, n, j) net = tflearn.residual_block(net, 1, j*2, downsample=True) net = tflearn.residual_block(net, n-1, j*2) net = tflearn.residual_block(net, 1, j*4, downsample=True) net = tflearn.residual_block(net, n-1, j*4) net = tflearn.residual_block(net, 1, j*8, downsample=True) net = tflearn.residual_block(net, n-1, j*8) net = tflearn.batch_normalization(net) net = tflearn.activation(net, 'relu') net = tflearn.global_avg_pool(net) ''' net = tflearn.conv_2d(net, j, 7, strides = 2, regularizer='L2', weight_decay=0.0001) net = max_pool_2d(net, 2, strides=2) net = tflearn.residual_block(net, n, j) net = tflearn.residual_block(net, 1, j*2, downsample=True) net = tflearn.residual_block(net, n-1, j*2) net = tflearn.residual_block(net, 1, j*4, downsample=True) net = tflearn.residual_block(net, n-1, j*4) net = tflearn.residual_block(net, 1, j*8, downsample=True) net = tflearn.residual_block(net, n-1, j*8) net = tflearn.batch_normalization(net) net = tflearn.activation(net, 'relu') net = tflearn.global_avg_pool(net) net = tflearn.fully_connected(net, len(yTest[0]), activation='softmax') mom = tflearn.Momentum(0.1, lr_decay=0.1, decay_step=32000, staircase=True) net = tflearn.regression(net, optimizer=mom, loss='categorical_crossentropy') model = tflearn.DNN(net, checkpoint_path='model2_resnet', max_checkpoints=10, tensorboard_verbose=3, clip_gradients=0.) model.load(_path) pred = model.predict(xTest) df = pd.DataFrame(pred) df.to_csv(_path + ".csv") newList = pred.copy() newList = convert2(newList) if _CSV: makeCSV(newList) pred = convert2(pred) pred = convert3(pred) yTest = convert3(yTest) print(metrics.confusion_matrix(yTest, pred)) print(metrics.classification_report(yTest, pred)) print('Accuracy', accuracy_score(yTest, pred)) print() if _wrFile: writeTest(pred)
def Define(): # less deep img_aug = tflearn.data_augmentation.ImageAugmentation() img_aug.add_random_flip_leftright() img_aug.add_random_crop((48, 48),6) img_aug.add_random_rotation(max_angle=30.) img_prep = tflearn.data_preprocessing.ImagePreprocessing() img_prep.add_featurewise_zero_center() img_prep.add_featurewise_stdnorm() n = 5 network = tflearn.input_data(shape=[None, 48, 48, 1], data_augmentation=img_aug, data_preprocessing=img_prep) #48 x 48 grayscale network = tflearn.conv_2d(network, 16, 3, regularizer='L2', weight_decay=0.0001) network = tflearn.residual_block(network, n, 16) network = tflearn.residual_block(network, 1, 32, downsample=True) network = tflearn.residual_block(network, n-1, 32) network = tflearn.residual_block(network, 1, 64, downsample=True) network = tflearn.residual_block(network, n-1, 64) network = tflearn.batch_normalization(network) network = tflearn.activation(network, 'relu') network = tflearn.global_avg_pool(network) # Regression network = tflearn.fully_connected(network, 7, activation='softmax') return network
def network(img_shape, name, LR): # Building Residual Network network = tflearn.input_data(shape=img_shape, name=name) network = tflearn.conv_2d(network, 16, 3, regularizer='L2', weight_decay=0.0001) network = tflearn.resnext_block(network, n, 16, 32) network = tflearn.resnext_block(network, 1, 32, 32, downsample=True) network = tflearn.resnext_block(network, n - 1, 32, 32) network = tflearn.resnext_block(network, 1, 64, 32, downsample=True) network = tflearn.resnext_block(network, n - 1, 64, 32) network = tflearn.batch_normalization(network) network = tflearn.activation(network, 'relu') network = tflearn.global_avg_pool(network) # Regression network = tflearn.fully_connected(network, 2, activation='softmax') opt = tflearn.Momentum(0.1, lr_decay=0.1, decay_step=32000, staircase=True) network = tflearn.regression(network, optimizer=opt, name='targets', loss='categorical_crossentropy') return network
def return2img(): n = 5 img_prep = tflearn.ImagePreprocessing() img_prep.add_featurewise_zero_center(per_channel=True) # Real-time data augmentation img_aug = tflearn.ImageAugmentation() img_aug.add_random_flip_leftright() img_aug.add_random_crop([32, 32], padding=4) # Building Residual Network net = tflearn.input_data(shape=[None, 32, 32, 1], data_preprocessing=img_prep, data_augmentation=img_aug) net = tflearn.conv_2d(net, 16, 3, regularizer='L2', weight_decay=0.0001) net = tflearn.residual_block(net, n, 16) net = tflearn.residual_block(net, 1, 32, downsample=True) net = tflearn.residual_block(net, n - 1, 32) net = tflearn.residual_block(net, 1, 64, downsample=True) net = tflearn.residual_block(net, n - 1, 64) net = tflearn.batch_normalization(net) net = tflearn.activation(net, 'relu') net = tflearn.global_avg_pool(net) # Regression net = tflearn.fully_connected(net, 3, activation='softmax') mom = tflearn.Momentum(0.1, lr_decay=0.1, decay_step=32000, staircase=True) net = tflearn.regression(net, optimizer=mom, loss='categorical_crossentropy') # Training model = tflearn.DNN(net) model.load("model_resnet_+-------cifar10-6500") img = load_img_label('/media/bai/Elements/LiangData_Afterchoose/img1') livermask = load_mask('/media/bai/Elements/LiangData_Afterchoose/mask1') abc=[] np.array(abc) for i in range(img.shape[0]): a = np.zeros((512,512,3,3),dtype=np.float32)#最后一项表示第几个分割方式,倒数第二项表示预测的三个结果 for j in range(0,3): this_mask = livermask[i, :, :, j] this_mask_num = int(np.max(this_mask)) for k in range(1,this_mask_num+1): if os.path.exists('/media/bai/Elements/LiangData_Afterchoose/superpixel/'+str(i)+'_'+str(j)+'_'+str(k)+'.jpg'): this_img = io.imread('/media/bai/Elements/LiangData_Afterchoose/superpixel/'+str(i)+'_'+str(j)+'_'+str(k)+'.jpg') this_img = np.reshape(this_img,(1,32,32,1)) this_img = this_img.astype(np.float32) result = model.predict(this_img) # print(result) abc.append(result) thisPartLoc=np.where(this_mask==k) for num in range(len(thisPartLoc[1])): a[thisPartLoc[0][num],thisPartLoc[1][num],0,j] = result[0,0] a[thisPartLoc[0][num],thisPartLoc[1][num], 1, j] = result[0,1] a[thisPartLoc[0][num],thisPartLoc[1][num], 2, j] = result[0,2] b = np.max(a,axis=3) final = np.argmax(b,axis=2) #添加3D CRF misc.imsave('/media/bai/Elements/LiangData_Afterchoose/result/' + str(i) + '.jpg', final)
def tflearn_imdb(): """ 文本情感分析 :return: """ (X_train, Y_train), (X_test, Y_test) = imdb.load_data() X_train, Y_train = pad_sequences(Y_train, maxlen=100), to_categorical(Y_train, nb_classes=2) X_test, Y_test = pad_sequences(Y_test, maxlen=100), to_categorical(Y_test, nb_classes=2) network = input_data([None, 100], name="input") tflearn.embedding(network, input_dim=10000, output_dim=128) branch1 = tflearn.conv_1d(network, 128, 3, padding="valid", activation="relu", regularizer="L2") branch2 = tflearn.conv_1d(network, 128, 4, padding="valid", activation="relu", regularizer="L2") branch3 = tflearn.conv_1d(network, 128, 5, padding="valid", activation="relu", regularizer="L2") network = tflearn.merge([branch1, branch2, branch3], mode="concat", axis=1) network = tf.expand_dims(network, 2) network = tflearn.global_avg_pool(network) network = tflearn.dropout(network, 0.5) network = tflearn.fully_connected(network, 2, activation="softmax") network = tflearn.regression(network, optimizer="adam", learning_rate=0.001, loss="categorical_crossentropy", name="target") model = tflearn.DNN(network, tensorboard_verbose=0) model.fit(X_train, Y_train, n_epoch=5, shuffle=True, validation_set=(X_test, Y_test), show_metric=True, batch_size=32)
def run(self): # Real-time pre-processing of the image data img_prep = ImagePreprocessing() img_prep.add_featurewise_zero_center() img_prep.add_featurewise_stdnorm() # Real-time data augmentation img_aug = tflearn.ImageAugmentation() img_aug.add_random_flip_leftright() # Resnet model below: Adapted from tflearn website self.n = 5 #32 layer resnet # Building Residual Network net = tflearn.input_data(shape=[None, 48, 48, 1], data_preprocessing=img_prep, data_augmentation=img_aug) net = tflearn.conv_2d(net, nb_filter=16, filter_size=3, regularizer='L2', weight_decay=0.0001) net = tflearn.residual_block(net, self.n, 16) net = tflearn.residual_block(net, 1, 32, downsample=True) net = tflearn.residual_block(net, self.n - 1, 32) net = tflearn.residual_block(net, 1, 64, downsample=True) net = tflearn.residual_block(net, self.n - 1, 64) net = tflearn.batch_normalization(net) net = tflearn.activation(net, 'relu') net = tflearn.global_avg_pool(net) # Regression net = tflearn.fully_connected(net, 7, activation='softmax') mom = tflearn.Momentum(learning_rate=0.1, lr_decay=0.0001, decay_step=32000, staircase=True, momentum=0.9) net = tflearn.regression(net, optimizer=mom, loss='categorical_crossentropy') self.model = tflearn.DNN(net, checkpoint_path='models/model_resnet_emotion', max_checkpoints=10, tensorboard_verbose=0, clip_gradients=0.) self.model.load('model.tfl') face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') cap = cv2.VideoCapture(0) #Main Loop where we will be capturing live webcam feed, crop image and process the image for emotion recognition on trained model while True: ret, img = cap.read() gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.3, 5) for (x, y, w, h) in faces: cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2) roi_gray = gray[y:y + h, x:x + w] roi_color = img[y:y + h, x:x + w] self.image_processing(roi_gray, img) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()
def build_net(): n = 5 tflearn.config.init_training_mode() img_prep = ImagePreprocessing() img_prep.add_featurewise_zero_center() img_prep.add_featurewise_stdnorm() # Real-time data augmentation img_aug = tflearn.ImageAugmentation() img_aug.add_random_flip_leftright() # img_aug.add_random_crop([48, 48], padding=8) # Building Residual Network net = tflearn.input_data(shape=[None, 48, 48, 1], data_preprocessing=img_prep, data_augmentation=img_aug) net = tflearn.conv_2d(net, nb_filter=16, filter_size=3, regularizer='L2', weight_decay=0.0001) net = tflearn.residual_block(net, n, 16) net = tflearn.residual_block(net, 1, 32, downsample=True) net = tflearn.residual_block(net, n - 1, 32) net = tflearn.residual_block(net, 1, 64, downsample=True) net = tflearn.residual_block(net, n - 1, 64) net = tflearn.batch_normalization(net) net = tflearn.activation(net, 'relu') net = tflearn.global_avg_pool(net) # Regression net = tflearn.fully_connected(net, 7, activation='softmax') mom = tflearn.Momentum(learning_rate=0.1, lr_decay=0.0001, decay_step=32000, staircase=True, momentum=0.9) net = tflearn.regression(net, optimizer=mom, loss='categorical_crossentropy') print("make model") model = tflearn.DNN( net, checkpoint_path='upload/Resmodels/model_resnet_emotion', max_checkpoints=10, tensorboard_verbose=0, clip_gradients=0.) print("load model start") model.load('upload/Resmodels/model_resnet_emotion-10500') print("load model success") return model
def inference(input_data, n_classes): #stem net = conv_2d(input_data, 32, 3, strides=2, activation='softplus', name='conv1') net = conv_2d(net, 32, 3, activation='softplus', name='conv2') net = conv_2d(net, 64, 3, activation='softplus', name='conv3') net1 = max_pool_2d(net, 3, strides=2, name='pool4') net2 = conv_2d(net, 96, 3, strides=2, activation='softplus', name='conv4') merge5 = tflearn.layers.merge_ops.merge([net1, net2], mode='concat', axis=3) net6_1 = conv_2d(merge5, 64, 1, activation='softplus', name='net6_1') net7_1 = conv_2d(net6_1, 96, 3, activation='softplus', name='net6_1') net6_2 = conv_2d(merge5, 64, 1, activation='softplus', name='net6_2') net7_2 = conv_2d(net6_2, 64, filter_size=[1, 7], activation='softplus', name='net7_2') net8_2 = conv_2d(net7_2, 64, filter_size=[7, 1], activation='softplus', name='net8_2') net10_2 = conv_2d(net8_2, 96, 3, activation='softplus', name='net10_2') merge = tflearn.layers.merge_ops.merge([net7_1, net10_2], mode='concat', axis=3) net11_1 = conv_2d(merge, 192, 3, activation='softplus', name='net11_1') net11_2 = max_pool_2d(merge, 1, name='net11_2') x = tflearn.layers.merge_ops.merge([net11_1, net11_2], mode='concat', axis=3) for i in range(3): x = inception_A(x) x = reduction_A(x) for i in range(5): x = inception_B(x) x = reduction_B(x) for i in range(3): x = inception_C(x) pool = tflearn.global_avg_pool(x) # softmax drop = tf.nn.dropout(pool, 0.8) soft = fully_connected(drop, n_classes, activation=None) soft = tf.nn.softmax(soft) return soft
def resnet1(x, n=5): net = tflearn.conv_2d(x, 16, 3, regularizer='L2', weight_decay=0.0001) net = tflearn.residual_block(net, n, 16) net = tflearn.residual_block(net, 1, 32, downsample=True) net = tflearn.residual_block(net, n - 1, 32) net = tflearn.residual_block(net, 1, 64, downsample=True) net = tflearn.residual_block(net, n - 1, 64) net = tflearn.batch_normalization(net) net = tflearn.activation(net, 'relu') net = tflearn.global_avg_pool(net) # Regression net = tflearn.fully_connected(net, 10, activation='softmax') return net
def resnet(inputs, prob_fc, prob_conv, wd, training_phase=True): n = 5 net = tflearn.conv_2d(inputs, 16, 3, regularizer='L2', weight_decay=0.0001) net = tflearn.residual_block(net, n, 16) net = tflearn.residual_block(net, 1, 32, downsample=True) net = tflearn.residual_block(net, n - 1, 32) net = tflearn.residual_block(net, 1, 64, downsample=True) net = tflearn.residual_block(net, n - 1, 64) net = tflearn.batch_normalization(net) net = tflearn.activation(net, 'relu') net = tflearn.global_avg_pool(net) # Regression net = tflearn.fully_connected(net, 10, activation='linear') return net
def get_model(model_name): # First we load the network print("Setting up neural networks...") n = 18 # Real-time data preprocessing print("Doing preprocessing...") img_prep = tflearn.ImagePreprocessing() img_prep.add_featurewise_zero_center( per_channel=True, mean=[0.573364, 0.44924123, 0.39455055]) # Real-time data augmentation print("Building augmentation...") img_aug = tflearn.ImageAugmentation() img_aug.add_random_flip_leftright() img_aug.add_random_crop([32, 32], padding=4) #Build the model (for 32 x 32) print("Shaping input data...") net = tflearn.input_data(shape=[None, 32, 32, 3], data_preprocessing=img_prep, data_augmentation=img_aug) net = tflearn.conv_2d(net, 16, 3, regularizer='L2', weight_decay=0.0001) print("Carving Resnext blocks...") net = tflearn.resnext_block(net, n, 16, 32) net = tflearn.resnext_block(net, 1, 32, 32, downsample=True) net = tflearn.resnext_block(net, n - 1, 32, 32) net = tflearn.resnext_block(net, 1, 64, 32, downsample=True) net = tflearn.resnext_block(net, n - 1, 64, 32) print("Erroding Gradient...") net = tflearn.batch_normalization(net) net = tflearn.activation(net, 'relu') net = tflearn.global_avg_pool(net) net = tflearn.fully_connected(net, 8, activation='softmax') opt = tflearn.Momentum(0.1, lr_decay=0.1, decay_step=32000, staircase=True) net = tflearn.regression(net, optimizer=opt, loss='categorical_crossentropy') print("Structuring model...") model = tflearn.DNN(net, tensorboard_verbose=0, clip_gradients=0.) # Load the model from checkpoint print("Loading the model...") model.load(model_name) return model
def ResNet1(network): network = conv_2d(network, 64, 3, activation='relu', bias=False) # Residual blocks network = residual_bottleneck(network, 3, 16, 64) network = residual_bottleneck(network, 1, 32, 128, downsample=True) network = residual_bottleneck(network, 2, 32, 128) network = residual_bottleneck(network, 1, 64, 256, downsample=True) network = residual_bottleneck(network, 2, 64, 256) network = batch_normalization(network) network = activation(network, 'relu') network = global_avg_pool(network) # Regression network = fully_connected(network, output_dim, activation='softmax') return network
def BReG_NeXt(_X): """BReG_NeXt implementation. Returns feature map before softmax. """ net = tflearn.conv_2d(_X, 32, 3, regularizer='L2', weight_decay=0.0001) net = residual_block(net, 7, 32, activation='elu') net = residual_block(net, 1, 64, downsample=True, activation='elu') net = residual_block(net, 8, 64, activation='elu') net = residual_block(net, 1, 128, downsample=True, activation='elu') net = residual_block(net, 7, 128, activation='elu') net = tflearn.batch_normalization(net) net = tflearn.activation(net, 'elu') net = tflearn.global_avg_pool(net) # Regression logits = tflearn.fully_connected(net, n_classes, activation='linear') return logits
def build_network(): # Real-time data preprocessing img_prep = ImagePreprocessing() img_prep.add_featurewise_zero_center(per_channel=True) # Real-time data augmentation img_aug = ImageAugmentation() img_aug.add_random_flip_leftright() img_aug.add_random_crop([IMG_WIDTH, IMG_HEIGHT], padding=4) #Ashis: transition layer didn't use here #Hence, each densenet_block used same nb_layers and growth (k) #transition layer needs to balance two adjacent densenet_block #by default, dropout is set as false. Downsample is used as True # Building Residual Network net = input_data(shape=[None, IMG_WIDTH, IMG_HEIGHT, CHANNELS], data_preprocessing=img_prep, data_augmentation=img_aug) net = conv_2d(net, 16, 3, regularizer='L2', weight_decay=0.0001) net = densenet_block(net, nb_layers, k) #no transition layer net = densenet_block(net, nb_layers, k) #no transition layer #net = densenet_block(net, nb_layers, k) #Ignore one #no transition layer net = tflearn.global_avg_pool(net) # Regression net = fully_connected(net, CLASS_NUMBER, activation='softmax') #opt = tflearn.optimizers.Nesterov(0.1, lr_decay=0.1, decay_step=32000, staircase=True) #opt = tflearn.optimizers.AdaGrad (learning_rate=0.01, initial_accumulator_value=0.01) net = regression(net, optimizer="adam", loss='categorical_crossentropy', learning_rate=0.001) # Training model = tflearn.DNN(net, checkpoint_path='model_densenet', max_checkpoints=10, tensorboard_verbose=0, clip_gradients=0.) return model
def resnet(dataset, img_prep, img_aug, X, Y, testX, testY, width, height, channel, class_num, filt, depth, epoch): # Building Residual Network layer = 1 net = tflearn.input_data(shape=[None, width, height, channel], data_preprocessing=img_prep, data_augmentation=img_aug) net = tflearn.conv_2d(net, filt, 3, regularizer='L2', weight_decay=0.0001) while (depth != 0): d_num = depth - (int)(depth / 100) * 100 depth = (int)(depth / 100) if (layer == 1): net = tflearn.residual_block(net, d_num, filt) else: net = tflearn.residual_block(net, 1, filt, downsample=True) net = tflearn.residual_block(net, d_num - 1, filt) layer = layer + 1 net = tflearn.batch_normalization(net) net = tflearn.activation(net, 'relu') net = tflearn.global_avg_pool(net) # Regression net = tflearn.fully_connected(net, class_num, activation='softmax') mom = tflearn.Momentum(0.1, lr_decay=0.1, decay_step=32000, staircase=True) net = tflearn.regression(net, optimizer=mom, loss='categorical_crossentropy') # Training model = tflearn.DNN(net, checkpoint_path=('model_resnet_' + dataset), max_checkpoints=10, tensorboard_verbose=0, clip_gradients=0.) model.fit(X, Y, n_epoch=epoch, validation_set=(testX, testY), snapshot_epoch=False, snapshot_step=500, show_metric=True, batch_size=128, shuffle=True, run_id=('resnet_' + dataset)) aa = model.predict(testX) correct = 0 for i in range(len(aa)): if (aa[i].index(max(aa[i])) == np.argmax(testY[i])): correct = correct + 1 return correct / len(aa)
def inference(x): n = 5 net = tflearn.conv_2d(x, 16, 3, regularizer='L2', weight_decay=0.0001) net = tflearn.resnext_block(net, n, 16, 32) net = tflearn.resnext_block(net, 1, 32, 32, downsample=True) net = tflearn.resnext_block(net, n - 1, 32, 32) net = tflearn.resnext_block(net, 1, 64, 32, downsample=True) net = tflearn.resnext_block(net, n - 1, 64, 32) net = tflearn.batch_normalization(net) net = tflearn.activation(net, 'relu') net = tflearn.global_avg_pool(net) # Regression net = tflearn.fully_connected(net, 5, activation=None) logits = net net = tflearn.activations.softmax(net) return logits, net
def ResNext1(network): # Residual blocks # 32 layers: n=5, 56 layers: n=9, 110 layers: n=18 n = 5 network = conv_2d(network, 16, 3, regularizer='L2', weight_decay=0.0001) network = resnext_block(network, n, 16, 32) network = resnext_block(network, 1, 32, 32, downsample=True) network = resnext_block(network, n - 1, 32, 32) network = resnext_block(network, 1, 32, 32, downsample=True) network = resnext_block(network, n - 1, 32, 32) network = batch_normalization(network) network = activation(network, 'relu') network = global_avg_pool(network) # Regression network = fully_connected(network, output_dim, activation='softmax') return network
def inference(inputs, n_class=1000, finetuning=False): block_list = [10, 20, 9] scale_list = [0.17, 0.10, 0.20] print(inputs) net = stem(inputs, scope='stem') print(net) # bloack A for i in range(block_list[0]): net = blockA(net, scale=scale_list[0], scope='A_' + str(i)) print(net) # reduction A net = reductionA(net) # bloack B for i in range(block_list[1]): net = blockB(net, scale=scale_list[1], scope='B_' + str(i)) print(net) # reduction B net = reductionB(net) print(net) # bloack C for i in range(block_list[2]): net = blockB(net, scale=scale_list[2], scope='C_' + str(i)) print(net) net = tflearn.global_avg_pool(net) print(net) keep_prob = tf.cond(tflearn.get_training_mode(), lambda: 1.0, lambda: 0.8) net = tflearn.dropout(net, keep_prob=keep_prob) with tf.variable_scope('FC', reuse=tf.AUTO_REUSE): net = tflearn.fully_connected(net, n_class, weights_init='uniform_scaling', regularizer='L2', weight_decay=0.0001, restore=(not finetuning)) print(net) return net
def _build_network(self, n, image_size): # Define the input to the network. img_prep = tflearn.ImagePreprocessing() img_prep.add_featurewise_zero_center(per_channel=True) # Real-time data augmentation. img_aug = tflearn.ImageAugmentation() img_aug.add_random_flip_leftright() net = tflearn.input_data(shape=[None, image_size[0], image_size[1], 3], data_preprocessing=img_prep, data_augmentation=img_aug) # Start with a normal convolutional layer. net = tflearn.conv_2d(net, 64, 3, regularizer='L2', weight_decay=0.0001) # Since this is a ResNet with <50 layers, we'll use regular residual blocks; # otherwise, we'd use residual bottleneck blocks instead. net = tflearn.residual_block(net, n, 64) net = tflearn.residual_block(net, 1, 128, downsample=True) net = tflearn.residual_block(net, n - 1, 128) net = tflearn.residual_block(net, 1, 256, downsample=True) net = tflearn.residual_block(net, n - 1, 256) # Perform batch normalization. net = tflearn.batch_normalization(net) # Activation at the end of the network pre-FC. net = tflearn.activation(net, 'relu') net = tflearn.global_avg_pool(net) net = tflearn.fully_connected(net, 2, activation='softmax') mom = tflearn.Momentum(0.1, lr_decay=0.1, decay_step=32000, staircase=True) net = tflearn.regression(net, optimizer=mom, loss='categorical_crossentropy') return net
def architecture03(input, num_class): net = tflearn.conv_2d(input, 64, 3, activation='relu', bias=False) net = tflearn.residual_bottleneck(net, 3, 16, 64) net = tflearn.residual_bottleneck(net, 1, 32, 128, downsample=False) net = tflearn.residual_bottleneck(net, 2, 32, 128) net = tflearn.residual_bottleneck(net, 1, 64, 256, downsample=False) net = tflearn.residual_bottleneck(net, 2, 64, 256) net = tflearn.batch_normalization(net) net = tflearn.activation(net, 'relu') net = tflearn.global_avg_pool(net) net = tflearn.fully_connected(net, num_class, activation='softmax') net = tflearn.regression(net, optimizer='momentum', loss='categorical_crossentropy', learning_rate=0.001) return tflearn.DNN(net, tensorboard_verbose=0)
def _model2(): global yTest, img_aug tf.reset_default_graph() img_prep = ImagePreprocessing() img_prep.add_featurewise_zero_center() img_prep.add_featurewise_stdnorm() net = input_data(shape=[None, inputSize, inputSize, dim], name='input', data_preprocessing=img_prep, data_augmentation=img_aug) n = 3 j = 64 ''' net = tflearn.conv_2d(net, j, 3, regularizer='L2', weight_decay=0.0001) net = tflearn.residual_block(net, n, j) net = tflearn.residual_block(net, 1, j*2, downsample=True) net = tflearn.residual_block(net, n-1, j*2) net = tflearn.residual_block(net, 1, j*4, downsample=True) net = tflearn.residual_block(net, n-1, j*4) net = tflearn.residual_block(net, 1, j*8, downsample=True) net = tflearn.residual_block(net, n-1, j*8) net = tflearn.batch_normalization(net) net = tflearn.activation(net, 'relu') net = tflearn.global_avg_pool(net) ''' net = tflearn.conv_2d(net, j, 7, strides = 2, regularizer='L2', weight_decay=0.0001) net = max_pool_2d(net, 2, strides=2) net = tflearn.residual_block(net, n, j) net = tflearn.residual_block(net, 1, j*2, downsample=True) net = tflearn.residual_block(net, n-1, j*2) net = tflearn.residual_block(net, 1, j*4, downsample=True) net = tflearn.residual_block(net, n-1, j*4) net = tflearn.residual_block(net, 1, j*8, downsample=True) net = tflearn.residual_block(net, n-1, j*8) net = tflearn.batch_normalization(net) net = tflearn.activation(net, 'relu') net = tflearn.global_avg_pool(net) net = tflearn.fully_connected(net, len(Y[0]), activation='softmax') mom = tflearn.Momentum(0.1, lr_decay=0.1, decay_step=32000, staircase=True) net = tflearn.regression(net, optimizer=mom, loss='categorical_crossentropy') model = tflearn.DNN(net, checkpoint_path='model_resnet_cifar10', max_checkpoints=10, tensorboard_verbose=3, clip_gradients=0.) model.fit(X, Y, n_epoch=epochNum, validation_set=(xTest, yTest),snapshot_epoch=False, snapshot_step=500, show_metric=True, batch_size=batchNum, shuffle=True, run_id= _id + 'artClassification') if modelStore: model.save(_id + '-model.tflearn')
def build_residual_network(self, network, res_n=5): # data_augmentation=self.generate_image_augumentation()) network = tflearn.conv_2d(network, 16, 3, regularizer='L2', weight_decay=0.0001) network = tflearn.residual_block(network, res_n, 16) network = tflearn.residual_block(network, 1, 32, downsample=True) network = tflearn.residual_block(network, res_n - 1, 32) network = tflearn.residual_block(network, 1, 64, downsample=True) network = tflearn.residual_block(network, res_n - 1, 64) network = tflearn.batch_normalization(network) network = tflearn.activation(network, 'relu') network = tflearn.global_avg_pool(network) # Regression network = tflearn.fully_connected(network, 2, activation='softmax') mom = tflearn.Momentum(0.1, lr_decay=0.1, decay_step=32000, staircase=True) network = tflearn.regression(network, optimizer=mom, loss='categorical_crossentropy') return network
def resnext(width, height, frame_count, lr, output=9, model_name = 'sentnet_color.model'): net = input_data(shape=[None, width, height, 3], name='input') net = tflearn.conv_2d(net, 16, 3, regularizer='L2', weight_decay=0.0001) net = tflearn.layers.conv.resnext_block(net, n, 16, 32) net = tflearn.resnext_block(net, 1, 32, 32, downsample=True) net = tflearn.resnext_block(net, n-1, 32, 32) net = tflearn.resnext_block(net, 1, 64, 32, downsample=True) net = tflearn.resnext_block(net, n-1, 64, 32) net = tflearn.batch_normalization(net) net = tflearn.activation(net, 'relu') net = tflearn.global_avg_pool(net) # Regression net = tflearn.fully_connected(net, output, activation='softmax') opt = tflearn.Momentum(0.1, lr_decay=0.1, decay_step=32000, staircase=True) net = tflearn.regression(net, optimizer=opt, loss='categorical_crossentropy') model = tflearn.DNN(net, max_checkpoints=0, tensorboard_verbose=0, tensorboard_dir='log') return model
testX = du.featurewise_zero_center(testX, mean) testX = du.featurewise_std_normalization(testX, std) Y = du.to_categorical(Y, 10) testY = du.to_categorical(testY, 10) # Building Residual Network net = tflearn.input_data(shape=[None, 32, 32, 3]) net = tflearn.conv_2d(net, 32, 3) net = tflearn.batch_normalization(net) net = tflearn.activation(net, 'relu') net = tflearn.shallow_residual_block(net, 4, 32, regularizer='L2') net = tflearn.shallow_residual_block(net, 1, 32, downsample=True, regularizer='L2') net = tflearn.shallow_residual_block(net, 4, 64, regularizer='L2') net = tflearn.shallow_residual_block(net, 1, 64, downsample=True, regularizer='L2') net = tflearn.shallow_residual_block(net, 5, 128, regularizer='L2') net = tflearn.global_avg_pool(net) # Regression net = tflearn.fully_connected(net, 10, activation='softmax') mom = tflearn.Momentum(0.1, lr_decay=0.1, decay_step=16000, staircase=True) net = tflearn.regression(net, optimizer=mom, loss='categorical_crossentropy') # Training model = tflearn.DNN(net, checkpoint_path='model_resnet_cifar10', max_checkpoints=10, tensorboard_verbose=0, clip_gradients=1.0) model.fit(X, Y, n_epoch=200, validation_set=(testX, testY), show_metric=True, batch_size=128, shuffle=True, run_id='resnet_cifar10')
#Load Test data image_count = (3,6) patch_count = 20 X = generate_patches(img2numpy_arr(config.test_path), image_count, patch_count) # Building Residual Network net = tl.input_data(shape=[None, 42, 42, 3]) net = tl.conv_2d(net, 32, 3) net = tl.batch_normalization(net) net = tl.activation(net, 'relu') net = tl.shallow_residual_block(net, 4, 32, regularizer='L2') net = tl.shallow_residual_block(net, 1, 32, downsample=True, regularizer='L2') net = tl.shallow_residual_block(net, 4, 64, regularizer='L2') net = tl.shallow_residual_block(net, 1, 64, downsample=True, regularizer='L2') net = tl.shallow_residual_block(net, 5, 64, regularizer='L2') net = tl.global_avg_pool(net) # Regression net = tl.fully_connected(net, 9, activation='softmax') mom = tl.Momentum(0.1, lr_decay=0.1, decay_step=16000, staircase=True) net = tl.regression(net, optimizer=mom, loss='categorical_crossentropy') # Training model = tl.DNN(net) model.load('resnet_analysis-172500') pred_y = model.predict(X) print(pred_y)