我如何与张量板连接

时间:2018-12-04 17:48:03

标签: tensorflow spyder tensorboard

import cv2                 # working with, mainly resizing, images
import numpy as np         # dealing with arrays
import os                  # dealing with directories
from random import shuffle # mixing up or currently ordered data that might.
from tqdm import tqdm      # a nice pretty percentage bar for tasks.  


TRAIN_DIR = 'train'
TEST_DIR = 'test'
IMG_SIZE = 50
LR = 1e-3

MODEL_NAME = 'snakes-{}-{}.model'.format(LR, '2conv-basic')

def label_img(img):
    print("\nImage = ",img)
    print("\n",img.split('.')[-2])
    temp_name= img.split('.')[-2]
    temp_name=temp_name[:1]
    word_label = temp_name



    if word_label == 'A': return [0,0,0,0,1]    #A_common_krait
    elif word_label == 'B': return [0,0,0,1,0]  #B_hump_nosed_viper
    elif word_label == 'C': return [0,0,1,0,0]  #C_indian_cobra
    elif word_label == 'D': return [0,1,0,0,0]  #D_russels_viper
    elif word_label == 'E' : return [1,0,0,0,0] #E_saw_scaled_viper


def create_train_data():
    training_data = []
    for img in tqdm(os.listdir(TRAIN_DIR)):
        label = label_img(img)
        path = os.path.join(TRAIN_DIR,img)
        img = cv2.imread(path,cv2.IMREAD_GRAYSCALE)
        img = cv2.resize(img, (IMG_SIZE,IMG_SIZE))
        training_data.append([np.array(img),np.array(label)])
    shuffle(training_data)
    np.save('train_data.npy', training_data)
    return training_data


def process_test_data():
    testing_data = []
    for img in tqdm(os.listdir(TEST_DIR)):
        path = os.path.join(TEST_DIR,img)
        img_num = img.split('.')[0]
        img = cv2.imread(path,cv2.IMREAD_GRAYSCALE)
        img = cv2.resize(img, (IMG_SIZE,IMG_SIZE))
        testing_data.append([np.array(img), img_num])
    shuffle(testing_data)
    np.save('test_data.npy', testing_data)
    return testing_data

train_data = create_train_data()


import tflearn
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression




import tensorflow as tf
tf.reset_default_graph()

convnet = input_data(shape=[None, IMG_SIZE, IMG_SIZE, 1], name='input')

convnet = conv_2d(convnet, 32, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)

convnet = conv_2d(convnet, 64, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)

convnet = conv_2d(convnet, 128, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)

convnet = conv_2d(convnet, 64, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)

convnet = conv_2d(convnet, 32, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)

convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, 0.8)

convnet = fully_connected(convnet, 5, activation='softmax')
convnet = regression(convnet, optimizer='adam', learning_rate=LR, 
loss='categorical_crossentropy', name='targets')

model = tflearn.DNN(convnet, tensorboard_dir='log')



if os.path.exists('{}.meta'.format(MODEL_NAME)):
    model.load(MODEL_NAME)
    print('model loaded!')


train = train_data[:-11200]
test = train_data[-11200:]

X = np.array([i[0] for i in train]).reshape(-1,IMG_SIZE,IMG_SIZE,1)
Y = [i[1] for i in train]

test_x = np.array([i[0] for i in test]).reshape(-1,IMG_SIZE,IMG_SIZE,1)
test_y = [i[1] for i in test]

model.fit({'input': X}, {'targets': Y}, n_epoch=3, validation_set=({'input': 
test_x}, {'targets': test_y}), 
    snapshot_step=500, show_metric=True, run_id=MODEL_NAME)



model.save(MODEL_NAME)
with open('submission_file.csv','w') as f:
    f.write('id,label\n')

with open('submission_file.csv','a') as f:
    for data in tqdm(test):
        img_num = data[1]
        img_data = data[0]
        orig = img_data
        data = img_data.reshape(IMG_SIZE,IMG_SIZE,1)
        model_out = model.predict([data])[0]
        f.write('{},{}\n'.format(img_num,model_out[1]))

该模型基于5种蛇和火车的分类,使用了17300张图像和11200张图像进行验证。因此我无法查看模型的性能,因为无法查看任何图形(准确性,损失,过度拟合等) 我想使用张量板可视化我的模型的工作流程,以及如何将该模型添加到张量板,任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

为什么您不能查看准确性,损失,过度拟合等图形的简单答案是因为您没有进行任何记录。 Tensorflow不会自动为您记录事物。

但是我强烈建议您阅读Tensorflow Mnist Example,尝试实现您认为应该的样子,然后参考Tensorflow MNIST with Tensorboard Example

如果您想快速入门这些概念,请查看quick introduction

答案 1 :(得分:0)

在某些情况下,我可以找到解决方案,

sess = tf.Session()
tf.summary.histogram
file_writer = tf.summary.FileWriter('./log', sess.graph)