# coding: utf-8 import tensorflow as tf import numpy as np import matplotlib.pyplot as plt import time import create_and_read_TFRecord2 as reader2 import os X_train, y_train = reader2.get_file("c:\\cat_and_dog_r") image_batch, label_batch = reader2.get_batch(X_train, y_train, 227, 227, 200, 2048) def batch_norm(inputs, is_training, is_conv_out=True, decay=0.999): scale = tf.Variable(tf.ones([inputs.get_shape()[-1]])) beta = tf.Variable(tf.zeros([inputs.get_shape()[-1]])) pop_mean = tf.Variable(tf.zeros([inputs.get_shape()[-1]]), trainable=False) pop_var = tf.Variable(tf.ones([inputs.get_shape()[-1]]), trainable=False) if is_training: if is_conv_out: batch_mean, batch_var = tf.nn.moments(inputs, [0, 1, 2]) else: batch_mean, batch_var = tf.nn.moments(inputs, [0]) train_mean = tf.assign(pop_mean, pop_mean * decay + batch_mean * (1 - decay)) train_var = tf.assign(pop_var,
# -*- coding: utf-8 -*- """ Created on Sat May 5 18:09:00 2018 @author: 11420 """ import numpy as np import tensorflow as tf import VGG16_model as model import create_and_read_TFRecord2 as reader2 if __name__ == '__main__': X_train, y_train = reader2.get_file("D:/Deep Code/defect-vs-normal/train/") image_batch, label_batch = reader2.get_batch(X_train, y_train, 224, 224, 100, 512) x_imgs = tf.placeholder(tf.float32, [None, 224, 224, 3]) y_imgs = tf.placeholder(tf.int32, [None, 2]) vgg = model.vgg16(x_imgs) fc3_defect_and_normal = vgg.probs loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=fc3_defect_and_normal, labels=y_imgs)) optimizer = tf.train.GradientDescentOptimizer( learning_rate=1e-4).minimize(loss) sess = tf.Session() sess.run(tf.global_variables_initializer())
def onehot(labels): n_sample = len(labels) n_class = max(labels) + 1 onehot_labels = np.zeros((n_sample, n_class)) onehot_labels[np.arange(n_sample), labels] = 1 return onehot_labels import VGG16_model as model import create_and_read_TFRecord2 as reader2 if __name__ == '__main__': print("111") print("222") X_train, y_train = reader2.get_file("afterClass/train") # 输入训练数据路径 image_batch, label_batch = reader2.get_batch(X_train, y_train, 224, 224, 25, 256) x_imgs = tf.placeholder(tf.float32, [None, 224, 224, 3]) y_imgs = tf.placeholder(tf.int32, [None, 2]) vgg = model.vgg16(x_imgs) fc3_cat_and_dog = vgg.probs loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=fc3_cat_and_dog, labels=y_imgs)) optimizer = tf.train.GradientDescentOptimizer( learning_rate=0.0001).minimize(loss) pre = tf.nn.softmax(fc3_cat_and_dog) correct_pred = tf.equal(tf.argmax(pre, 1), tf.argmax(y_imgs, 1)) accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) sess = tf.Session()
import tensorflow as tf import numpy as np import time import matplotlib.pyplot as plt import create_and_read_TFRecord2 as reader2 learning_rate = 1e-4 training_iters = 2000 batch_size = 64 dispaly_step = 5 n_classes = 2 n_fc1 = 4096 n_fc2 = 2048 X_train, y_train = reader2.get_file("./data") image_batch, label_batch = reader2.get_batch(X_train, y_train, 227, 227, batch_size, 512) x = tf.placeholder(tf.float32, [None, 227, 227, 3]) y = tf.placeholder(tf.int32, [None, n_classes]) W_conv = { 'conv1': tf.Variable(tf.truncated_normal([11, 11, 3, 96], stddev=0.01)), 'conv2': tf.Variable(tf.truncated_normal([5, 5, 96, 256], stddev=0.01)), 'conv3': tf.Variable(tf.truncated_normal([3, 3, 256, 384], stddev=0.01)), 'conv4': tf.Variable(tf.truncated_normal([3, 3, 384, 384], stddev=0.01)), 'conv5': tf.Variable(tf.truncated_normal([3, 3, 384, 256], stddev=0.01)), 'fc1': tf.Variable(tf.truncated_normal([6 * 6 * 256, n_fc1], stddev=0.01)), 'fc2': tf.Variable(tf.truncated_normal([n_fc1, n_fc2], stddev=0.01)), 'fc3': tf.Variable(tf.truncated_normal([n_fc2, n_classes], stddev=0.01)) }
# coding: utf-8 import tensorflow as tf import numpy as np import matplotlib.pyplot as plt import time import create_and_read_TFRecord2 as reader2 X_train, y_train = reader2.get_file("../Pulmonary_nodules_data/train/") image_batch, label_batch = reader2.get_batch(X_train, y_train, 227, 227, 200, 2048) def batch_norm(inputs, is_training, is_conv_out=True, decay=0.999): scale = tf.Variable(tf.ones([inputs.get_shape()[-1]])) beta = tf.Variable(tf.zeros([inputs.get_shape()[-1]])) pop_mean = tf.Variable(tf.zeros([inputs.get_shape()[-1]]), trainable=False) pop_var = tf.Variable(tf.ones([inputs.get_shape()[-1]]), trainable=False) if is_training: if is_conv_out: batch_mean, batch_var = tf.nn.moments(inputs, [0, 1, 2]) else: batch_mean, batch_var = tf.nn.moments(inputs, [0]) train_mean = tf.assign(pop_mean, pop_mean * decay + batch_mean * (1 - decay)) train_var = tf.assign(pop_var, pop_var * decay + batch_var * (1 - decay)) with tf.control_dependencies([train_mean, train_var]): return tf.nn.batch_normalization(inputs, batch_mean, batch_var, beta, scale, 0.001) else: