def main(args): style= args.style #img_width = img_height = args.image_size output_file =args.output input_file = args.input original_color = args.original_color blend_alpha = args.blend media_filter = args.media_filter aspect_ratio, x = preprocess_reflect_image(input_file, size_multiple=4) img_width= img_height = x.shape[1] net = nets.image_transform_net(img_width,img_height) z = concatenate([net.output, net.input], axis=0) model = VGG16(include_top=False,input_tensor=z) #model.summary() #model.compile(Adam(), dummy_loss) # Dummy loss since we are learning from regularizes model.load_weights("pretrained/"+style+'_weights.h5',by_name=False) t1 = time.time() y = net.predict(x)[0] print("process: %s" % (time.time() -t1)) imsave('%s_output.png' % output_file, y) net.save('keras.h5')
def loss_net(x_in, trux_x_in, width, height, style_image_path, content_weight, style_weight): # Append the initial input to the FastNet input to the VGG inputs x = concatenate([x_in, trux_x_in], axis=0) # Normalize the inputs via custom VGG Normalization layer x = VGGNormalize(name="vgg_normalize")(x) vgg = VGG16(include_top=False, input_tensor=x, weights='imagenet') vgg_output_dict = dict([(layer.name, layer.output) for layer in vgg.layers[-18:]]) vgg_layers = dict([(layer.name, layer) for layer in vgg.layers[-18:]]) if style_weight > 0: add_style_loss(vgg, style_image_path, vgg_layers, vgg_output_dict, width, height, style_weight) if content_weight > 0: add_content_loss(vgg_layers, vgg_output_dict, content_weight) # Freeze all VGG layers for layer in vgg.layers[-19:]: layer.trainable = False return vgg
def loss_net(x_in, trux_x_in, width, height, style_image_path, content_weight, style_weight): """ Fixed loss network: Pre-trained VGG16 on ImageNet. """ # Append the initial input to the FastNet input to the VGG inputs x = concatenate([x_in, trux_x_in], axis=0) # Normalize the inputs via custom VGG Normalization layer x = VGGNormalize(name="vgg_normalize")(x) # VGG network instance vgg = VGG16(include_top=False, input_tensor=x) # Populate VGG network vgg_output_dict = dict([(layer.name, layer.output) for layer in vgg.layers[-18:]]) vgg_layers = dict([(layer.name, layer) for layer in vgg.layers[-18:]]) # Style loss if style_weight > 0: add_style_loss(vgg, style_image_path, vgg_layers, vgg_output_dict, width, height, style_weight) # Content loss if content_weight > 0: add_content_loss(vgg_layers, vgg_output_dict, content_weight) # Freeze all VGG layers for layer in vgg.layers[-19:]: layer.trainable = False return vgg
def get_means(img): vgg = VGG16(img, weight_path='../data/models/vgg16.npy') conv1_1_mean = tf.squeeze(tf.reduce_mean(vgg.conv1_1, axis=-1), axis=0) conv1_2_mean = tf.squeeze(tf.reduce_mean(vgg.conv1_2, axis=-1), axis=0) conv2_1_mean = tf.squeeze(tf.reduce_mean(vgg.conv2_1, axis=-1), axis=0) conv2_2_mean = tf.squeeze(tf.reduce_mean(vgg.conv2_2, axis=-1), axis=0) conv3_1_mean = tf.squeeze(tf.reduce_mean(vgg.conv3_1, axis=-1), axis=0) conv3_2_mean = tf.squeeze(tf.reduce_mean(vgg.conv3_2, axis=-1), axis=0) conv3_3_mean = tf.squeeze(tf.reduce_mean(vgg.conv3_3, axis=-1), axis=0) conv4_1_mean = tf.squeeze(tf.reduce_mean(vgg.conv4_1, axis=-1), axis=0) conv4_2_mean = tf.squeeze(tf.reduce_mean(vgg.conv4_2, axis=-1), axis=0) conv4_3_mean = tf.squeeze(tf.reduce_mean(vgg.conv4_3, axis=-1), axis=0) conv5_1_mean = tf.squeeze(tf.reduce_mean(vgg.conv5_1, axis=-1), axis=0) conv5_2_mean = tf.squeeze(tf.reduce_mean(vgg.conv5_2, axis=-1), axis=0) conv5_3_mean = tf.squeeze(tf.reduce_mean(vgg.conv5_3, axis=-1), axis=0) means = [ conv1_1_mean, conv1_2_mean, conv2_1_mean, conv2_2_mean, conv3_1_mean, conv3_2_mean, conv3_3_mean, conv4_1_mean, conv4_2_mean, conv4_3_mean, conv5_1_mean, conv5_2_mean, conv5_3_mean ] return means
def face_classifier_did_loaded(): fc = Face_Classifier_With_Tensorflow() img_cv = PickleHelper.load_pickle("../../../Data/Face/", "faces-obj-32x32-features-norm.pkl") img_label = PickleHelper.load_pickle("../../../Data/Face/", "faces-obj-32x32-labels-norm.pkl") print("\nFEATURE SHAPE: {0}, LABEL SHAPE: {1}\n".format( np.shape(img_cv), np.shape(img_label))) ''' # JUST FOR TEST np.random.seed(32) img_label = np.random.randint(2, size=len(img_label)) print(img_label) #fc.imshow(img_cv[0]) ''' ''' test_idx = 2 print("Label: {0} = < {1} | {2} >".format(img_label[test_idx], np.max(img_cv[test_idx]), np.min(img_cv[test_idx]))) #print(img_cv[test_idx][:30, :30]) fc.imshow(img_cv[test_idx]) ''' vgg16 = VGG16(img_cv[:100], img_label[:100]) vgg16.run_architecture()
def Multimodel(cnn_weights_path=None, all_weights_path=None, class_num=5, cnn_no_vary=False): """ 获取densent121,xinception并联的网络 此处的cnn_weights_path是个列表是densenet和xception的卷积部分的权值 """ input_layer = Input(shape=(img_width, img_height, img_channel)) vgg_notop = VGG16(include_top=False, weights='imagenet', input_tensor=None, input_shape=(img_width, img_height, img_channel)) xception_notop = Xception(include_top=False, weights='imagenet', input_tensor=None, input_shape=(img_width, img_height, img_channel)) # res=ResNet50(include_top=False,weights=None,input_shape=(224,224,3)) if cnn_no_vary: for i, layer in enumerate(vgg_notop.layers): vgg_notop.layers[i].trainable = False for i, layer in enumerate(xception_notop.layers): xception_notop.layers[i].trainable = False # for i,layer in enumerate(res.layers): # res.layers[i].trainable=False if cnn_weights_path is not None: vgg_notop.load_weights(cnn_weights_path[0]) xception_notop.load_weights(cnn_weights_path[1]) # res.load_weights(cnn_weights_path[2]) vgg = vgg_notop(input_layer) xception = xception_notop(input_layer) # 对dense_121和xception进行全局最大池化 top1_model = GlobalMaxPooling2D(data_format='channels_last')(vgg) top2_model = GlobalMaxPooling2D(data_format='channels_last')(xception) # top3_model=GlobalMaxPool2D(input_shape=res.output_shape)(res.outputs[0]) print(top1_model.shape, top2_model.shape) # 把top1_model和top2_model连接起来 t = Concatenate(axis=1)([top1_model, top2_model]) # 第一个全连接层 top_model = Dense(units=512, activation="relu")(t) top_model = Dropout(rate=0.5)(top_model) top_model = Dense(units=class_num, activation="softmax")(top_model) model = Model(inputs=input_layer, outputs=top_model) # 加载全部的参数 if all_weights_path: model.load_weights(all_weights_path) return model
def creatModel(ModelName, x_imgs): # 创建模型 model = "" if ModelName == "VGG16": model = VGG16(x_imgs) elif ModelName == "ResNet": pass elif ModelName == "InceptionNet": pass else: raise Exception("Error Input~") return model
img = tf.image.decode_jpeg(img) #注意此处为jpeg格式 img = tf.image.resize(img, size) / 255.0 return (img, label) ds_train = tf.data.Dataset.list_files("data/train/*/*.jpg") \ .map(load_img,num_parallel_calls=tf.data.experimental.AUTOTUNE) \ .shuffle(buffer_size=1000).batch(BATCH_SIZE) \ .prefetch(tf.data.experimental.AUTOTUNE) ds_test = tf.data.Dataset.list_files("data/test/*/*.jpg") \ .map(load_img,num_parallel_calls=tf.data.experimental.AUTOTUNE) \ .shuffle(buffer_size=1000).batch(BATCH_SIZE) \ .prefetch(tf.data.experimental.AUTOTUNE) # 获得模型 model = VGG16(NUM_CLASS) # 注意要开启skip_mismatch和by_name model.load_weights("model/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5", skip_mismatch=True, by_name=True) # 编译模型 model.compile(optimizer='adam', loss='binary_crossentropy', metrics=["accuracy"]) cp_callback = ModelCheckpoint( 'logs/ep{epoch:02d}-loss{loss:.2f}.h5', monitor='acc', save_weights_only=True, ) # 训练模型 history = model.fit(
import torch import sys import os import numpy as np from VGG16 import VGG16 if __name__ == '__main__': pascalClasses = np.asarray(['__background__', 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor']) checkpoint = torch.load('faster_rcnn_1_6_10021.pth', map_location=(lambda storage, loc: storage)) print(checkpoint) fasterRCNN = VGG16(pascalClasses) fasterRCNN.load_state_dict(checkpoint['model']) fasterRCNN.createArchitecture()
EPOCH = 1 BATCH_SIZE = 10 # 批处理数量 TIME_STEP = 224 # rnn time step /image height INPUT_SIZE = 224 # rnn input step /image width LR = 0.01 # learning rate # 设置自己的装载器 if __name__ == '__main__': dataset = TextDataLoader('picture_data/train') with torch.no_grad(): model = VGG16() loss_func = torch.nn.CrossEntropyLoss() opt = torch.optim.Adam(model.parameters(), lr=0.01) train_dataset = Data.DataLoader( dataset=dataset, batch_size=16, shuffle=True ) model.cpu() model.train() # model.cuda() for epoch_times in range(60): loss_count = [] #异常处理
transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]), ]) train_loader = DataLoader(cifar.CIFAR10(root='cifar', train=True, transform=transform, download=True), batch_size=args.batch_size, shuffle=False, pin_memory=True, drop_last=True) test_loader = DataLoader(cifar.CIFAR10(root='cifar', train=False, transform=transform, download=True), batch_size=args.batch_size, shuffle=False, pin_memory=True, drop_last=True) loss_fn = torch.nn.CrossEntropyLoss().cuda() result = {} result['train_time'] = [] result['train_mem'] = [] result['train_loss'] = [] result['test_time'] = [] result['test_loss'] = [] result['test_acc'] = [] for i in range(5): model = VGG16(num_classes=10).cuda() model.train() optimizer = torch.optim.SGD(model.parameters(), lr=0.01, momentum=0.9) if args.mode == 'FP16': model = network_to_half(model) optimizer = FP16_Optimizer(optimizer, static_loss_scale=128) elif args.mode == 'amp': model, optimizer = amp.initialize(model, optimizer, opt_level=args.opt_level) ll = [] iteration = 0 start_time = time.time() while not iteration == args.iteration: for x, y in train_loader: x, y = x.cuda(), y.cuda() if args.mode == 'FP16':
batch_size=50, shuffle=False, num_workers=2) # Dataloader classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck') # %% # I have commented out some lines which I just use during experimentation. import numpy as np #dataiter = iter(trainloader) #images, labels = dataiter.next() from VGG16 import VGG16 # Import network from VGG script model = VGG16(10, nn.ReLU()).cuda( ) # Initialize model with 10 classes and relu activation function. # Note that if you run this scipt on a cpu you must remove the ".cuda()" # from this line. #out, layers = model(images) # Nevermind this part, it is just for prototyping and experimenting. # %% # #mi_mat = np.zeros((1, 17, 17)) #j_mat = np.zeros((1, 17, 17)) # #for i, layer in enumerate(layers, 0): # # mi_mat[0, 0, 0] = model.mutual_information(images, images) # mi_mat[0, 0, i+1] = model.mutual_information(images, layer.cpu())
#print(train_dataset.imgs) train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=BARCH_SIZE, shuffle=True) # 从文件夹中读取validation数据 validation_dataset = torchvision.datasets.ImageFolder(root='./data/test', transform=transform) #print(validation_dataset.class_to_idx) test_loader = torch.utils.data.DataLoader(validation_dataset, batch_size=64, shuffle=True) alexNet = VGG16(2).to(device) #alexNet = alexNet(pretrained=True) #alexNet = torch.load("./alexNet.pth") criterion = nn.CrossEntropyLoss() opti = torch.optim.Adam(alexNet.parameters(), lr=LR) if __name__ == '__main__': Accuracy_list = [] Loss_list = [] for epoch in range(EPOCH): sum_loss = 0.0 correct1 = 0 total1 = 0 for i, (images, labels) in enumerate(train_loader):
def loss_net(x_in, trux_x_in, width, height, style_image_path, content_weight, style_weight): # Append the initial input to the FastNet input to the VGG inputs x = concatenate([x_in, trux_x_in], axis=0) vgg = VGG16(include_top=False, input_tensor=x) return vgg
test_image_tensor = transform(test_image) if torch.cuda.is_available(): test_image_tensor = test_image_tensor.view(1, 3, 224, 224).cuda() else: test_image_tensor = test_image_tensor.view(1, 3, 224, 224) with torch.no_grad(): model.eval() out = model(test_image_tensor) ps = torch.exp(out) topk, topclass = ps.topk(1, dim=1) print("Prediction : ", idx_to_class[topclass.cpu().numpy()[0][0]], ", Score: ", topk.cpu().numpy()[0][0]) text = idx_to_class[topclass.cpu().numpy()[0][0]] + " " + str( topk.cpu().numpy()[0][0]) font = ImageFont.truetype('arial.ttf', 36) draw.text((0, 0), text, (255, 0, 0), font=font) test_image.show() #model = torch.load('./alexNet.pth', map_location='cpu')#使用cpu去推理 model = VGG16(2) model.load_state_dict(torch.load('./params.pth', map_location='cpu')) #使用cpu去推理 loss_func = nn.NLLLoss() predict(model, './data/test/drink/1_6_702.jpg') computeTestSetAccuracy(model, loss_func)
def __init__(self, scope_name, image_width = 224, image_height = 224, image_depth = 1, num_labels = 17, learning_rate = 0.05, bn_option = 0, phase=0): self.scope_name = scope_name self.image_width = image_width self.image_height = image_height self.image_depth = image_depth self.num_labels = num_labels self.learning_rate = learning_rate self.bn_option = bn_option # 0=no bn, 1=bn before ReLU, 2=bn after ReLU self.phase = phase # 0 = test / inference, 1 = training with tf.variable_scope(self.scope_name): self.holistic_model = VGG16(scope_name="holistic", image_width = self.image_width, image_height = self.image_height, image_depth = self.image_depth, num_labels = self.num_labels, bn_option=self.bn_option, phase=self.phase) self.header_model = VGG16(scope_name="header", image_width = self.image_width, image_height = self.image_height, image_depth = self.image_depth, num_labels = self.num_labels, bn_option=self.bn_option, phase=self.phase) self.footer_model = VGG16(scope_name="footer", image_width = self.image_width, image_height = self.image_height, image_depth = self.image_depth, num_labels = self.num_labels, bn_option=self.bn_option, phase=self.phase) self.left_model = VGG16(scope_name="left", image_width = self.image_width, image_height = self.image_height, image_depth = self.image_depth, num_labels = self.num_labels, bn_option=self.bn_option, phase=self.phase) self.right_model = VGG16(scope_name="right", image_width = self.image_width, image_height = self.image_height, image_depth = self.image_depth, num_labels = self.num_labels, bn_option=self.bn_option, phase=self.phase) # Placeholders self.holistc_tf_data = self.holistic_model.tf_data self.header_tf_data = self.header_model.tf_data self.footer_tf_data = self.footer_model.tf_data self.left_tf_data = self.left_model.tf_data self.right_tf_data = self.right_model.tf_data # softmax output for all 5 regions self.holistic_output = self.holistic_model.softmax self.header_output = self.header_model.softmax self.footer_output = self.footer_model.softmax self.left_output = self.left_model.softmax self.right_output = self.right_model.softmax self.tf_labels = tf.placeholder(tf.float32, shape = (None, self.num_labels)) # concatenate all softmax layers [batch_size, num_labels * 5] self.concat_output = tf.concat(values=[self.holistic_output, self.header_output, self.footer_output, self.left_output, self.right_output], axis=-1) # META CLASSIFIER # WEIGHTS META_NUM_HIDDEN_1, META_NUM_HIDDEN_2 = 256, 256 EPSILON = 1e-3 # For batch normalization ops self.w1 = tf.Variable(name="w1", initial_value=tf.truncated_normal([num_labels * 5, META_NUM_HIDDEN_1], stddev=0.1)) self.b1 = tf.Variable(name="b1", initial_value=tf.constant(1.0, shape = [META_NUM_HIDDEN_1])) self.w2 = tf.Variable(name="w2", initial_value=tf.truncated_normal([META_NUM_HIDDEN_1, META_NUM_HIDDEN_2], stddev=0.1)) self.b2 = tf.Variable(name="b2", initial_value=tf.constant(1.0, shape = [META_NUM_HIDDEN_2])) self.w3 = tf.Variable(name="w3", initial_value=tf.truncated_normal([META_NUM_HIDDEN_2, self.num_labels], stddev=0.1)) self.b3 = tf.Variable(name="b3", initial_value=tf.constant(1.0, shape = [self.num_labels])) # LAYERS # Layer 1 self.meta1_fccd = tf.matmul(self.concat_output, self.w1) + self.b1 self.meta1_relu = tf.nn.relu(self.meta1_fccd) self.meta1_mean, self.meta1_var = tf.nn.moments(self.meta1_relu, [0]) self.meta1_bn = tf.nn.batch_normalization(x=self.meta1_relu, mean=self.meta1_mean, variance=self.meta1_var, offset=None, scale=None, variance_epsilon=EPSILON) # Layer 2 self.meta2_fccd = tf.matmul(self.meta1_bn, self.w2) + self.b2 self.meta2_relu = tf.nn.relu(self.meta2_fccd) self.meta2_mean, self.meta2_var = tf.nn.moments(self.meta2_relu, [0]) self.meta2_bn = tf.nn.batch_normalization(x=self.meta2_relu, mean=self.meta2_mean, variance=self.meta2_var, offset=None, scale=None, variance_epsilon=EPSILON) # Layer 3 self.logits = tf.matmul(self.meta2_bn, self.w3) + self.b3 self.softmax = tf.nn.softmax(self.logits) self.argmax = tf.argmax(self.softmax, 1) # Training ops self.loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=self.logits, labels=self.tf_labels)) # self.optimizer_op = tf.train.AdamOptimizer(learning_rate=self.learning_rate).minimize(self.loss_op) self.optimizer = tf.train.GradientDescentOptimizer(self.learning_rate) # Gradient Clipping self.gradients = self.optimizer.compute_gradients(self.loss_op) self.capped_gradients = [(tf.clip_by_value(grad, -1., 1.), var) for grad, var in self.gradients if grad is not None] self.train_op = self.optimizer.apply_gradients(self.capped_gradients) # Label Argmax self.label_argmax = tf.argmax(self.tf_labels, 1) # Global Step self.global_step = tf.Variable(0, trainable=False, name='global_step')
img = img[np.newaxis, :, :, :] #print(type(img)) img = img[:, :, :, ::-1] #rgb->bgr img = img.transpose(0, 3, 1, 2) #use openCV #mean = np.array([103.939, 116.779, 123.68]) #img = cv.imread(args.image).astype(np.float32) #img -= mean #img = cv.resize(img, (224, 224)).transpose((2, 0, 1)) #img = img[np.newaxis, :, :, :] #img = img[:, :, :, ::-1] #brg->rgb #print(img.shape) vgg = VGG16() serializers.load_hdf5('VGG.model', vgg) #bias->0 #print(vgg.conv1_1.b.data) #print(type(vgg.conv1_1.b.data)) #vgg.conv1_1.b.data = np.zeros_like(vgg.conv1_1.b.data) #print(vgg.conv1_1.b.data) #vgg.conv1_2.b.data = np.zeros_like(vgg.conv1_2.b.data) #vgg.conv2_1.b.data = np.zeros_like(vgg.conv2_1.b.data) #vgg.conv2_2.b.data = np.zeros_like(vgg.conv2_2.b.data) #vgg.conv3_1.b.data = np.zeros_like(vgg.conv3_1.b.data) #vgg.conv3_2.b.data = np.zeros_like(vgg.conv3_2.b.data) #vgg.conv3_3.b.data = np.zeros_like(vgg.conv3_3.b.data) #vgg.conv4_1.b.data = np.zeros_like(vgg.conv4_1.b.data) #vgg.conv4_2.b.data = np.zeros_like(vgg.conv4_2.b.data)
file_path_v = 'dataset/V_228.csv' time_slot = 2 * 12 - 1 predict_slot = 1 data_v = np.array(pd.read_csv(file_path_v, header=None).values) part_len = len(data_v) // 10 train_data_v = data_v[: part_len * 9] test_data_v = data_v[part_len * 9:] train_set = dataset(train_data_v, time_slot, predict_slot, BATCH_SIZE) test_set = dataset(test_data_v, time_slot, predict_slot, BATCH_SIZE) train_loader = DataLoader(dataset=train_set, batch_size=BATCH_SIZE, shuffle=True) test_loader = DataLoader(dataset=test_set, batch_size=BATCH_SIZE, shuffle=True) vgg16 = VGG16(num_classes=228) vgg16.to(device) # Loss and Optimizer cost = tnn.MSELoss() optimizer = torch.optim.Adam(vgg16.parameters(), lr=LEARNING_RATE) scheduler = StepLR(optimizer, step_size=1, gamma=0.95) # torch.save(vgg16, 'test.pt') # evaluate model def evaluate(loader): vgg16.eval() predictions = [] labels = [] with torch.no_grad():
def _mean_image_subtraction(image): means = [123.69, 116.78, 103.69] if image.get_shape().ndims!=3: raise ValueError('Input must be of size[height, width, C>0]') num_channels = image.get_shape().as_list()[-1] if len(means)!=num_channels: raise ValueError('len(means) nust match the number of channels') channels = tf.split(image, num_or_size_splits=num_channels, axis=2) for i in range(num_channels): channels[i] -= means[i] return tf.concat(axis=2, values=channels) # 扩维 image = tf.expand_dims(_mean_image_subtraction(image_data), axis=0) with tf.Session() as sess: images = tf.placeholder(tf.float32, [1, 224, 224, 3]) fc_rate = tf.placeholder(tf.float32) image = sess.run(image) feed_dict = {images: image, fc_rate: 1.0} ckpt = tf.train.get_checkpoint_state(checkpoint_path) vgg = VGG16(vgg16_npy_path, trainable=False) vgg.build(images, 5, fc_rate) saver = tf.train.Saver() if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) print('Model restored ... fc6, fc7, fc8') prob = sess.run(vgg.prop, feed_dict=feed_dict) print(prob)
N = 5 batch_size_tr = 100 batch_size_te = 100 epochs = 50 tr_size = 50000 n_iterations = (tr_size // batch_size_tr) * epochs activation = 'relu' x_tr, y_tr, x_te, y_te = load_cifar10(path, gpu) for n in range(N): current_iteration = 0 if gpu: model = VGG16(10, activation, n_iterations).cuda() else: model = VGG16(10, activation, n_iterations) for epoch in range(epochs): batches_tr = list(model.make_batches(tr_size, batch_size_tr)) for idx_tr in batches_tr: x_tr_b = x_tr[idx_tr] y_tr_b = y_tr[idx_tr] idx_te = random.sample(range(0, 10000), batch_size_te) x_te_b = x_tr_b #x_te[idx_te]
classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck') # %% # I have commented out some lines which I just use during experimentation. import numpy as np #dataiter = iter(trainloader) #images, labels = dataiter.next() from VGG16 import VGG16 # Import network from VGG script #model = VGG16(10, nn.ReLU()).cuda()# Initialize model with 10 classes and relu activation function. # Note that if you run this scipt on a cpu you must remove the ".cuda()" # from this line. model = VGG16(10, nn.Sigmoid()).cuda() #out, layers = model(images) # Nevermind this part, it is just for prototyping and experimenting. # %% # #mi_mat = np.zeros((1, 17, 17)) #j_mat = np.zeros((1, 17, 17)) # #for i, layer in enumerate(layers, 0): # # mi_mat[0, 0, 0] = model.mutual_information(images, images) # mi_mat[0, 0, i+1] = model.mutual_information(images, layer.cpu()) # # j_mat[0, 0, 0] = model.joint_renyi(images, images)
# Label classes classes = {0:"memo", 1:"form", 2:"email", 3:"handwritten", 4:"advertisement",\ 5:"scientific report", 6:"scientific publication", 7:"specification", \ 8: "file folder", 9:"news article", 10:"budget", 11:"invoice", \ 12:"presentation", 13:"questionnaire", 14:"resume", 15:"memo", 16:"Tobacco800"} class_l = list(classes.values()) # Graph graph = tf.Graph() with graph.as_default(): # Normal model = VGG16(scope_name="vgg_16", image_width=224, image_height=224, image_depth=3, num_labels=17, bn_option=2, phase=phase) # model = VGG16_concat(scope_name="holistic", image_width = 224, image_height = 224, image_depth = 3, num_labels = 17, bn_option=2, phase=phase) # model = VGG16_selu(scope_name="holistic", image_width = 224, image_height = 224, image_depth = 3, num_labels = 17, phase=phase) variables = model.get_variables() logits_op = model.logits softmax_op = model.softmax argmax_op = model.argmax # loss_op, optimizer_op, global_step, label_argmax = model.training_step(learning_rate=learning_rate, learning_rate_type=learning_rate_type, optimizer_type=optimizer_type) loss_op, optimizer_op, global_step, label_argmax = model.training_step( learning_rate=0.01, learning_rate_type="STATIC", optimizer_type="RMSPROP")
def main(): # Dataset path # create_data_sets() train_list = 'train.txt' test_list = 'test.txt' # Learning params learning_rate = 0.0005 training_iters = 7000 # 10 epochs batch_size = 10 display_step = 20 test_step = 100 # 0.5 epoch save_step = 1000 # Network params n_classes = 6 keep_rate = 0.5 # Graph input x = tf.placeholder(tf.float32, [batch_size, 224, 224, 3]) y = tf.placeholder(tf.float32, [None, n_classes]) keep_var = tf.placeholder(tf.float32) # Model # pred = Model.alexnet(x, keep_var) vgg = VGG16(x) pred = vgg.getVGG16() # pred = resnet({'data': x}).layers["fc1000"] # Loss and optimizer loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y)) optimizer = tf.train.GradientDescentOptimizer( learning_rate=learning_rate).minimize(loss) # Evaluation correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1)) accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) # Init init = tf.initialize_all_variables() # Load dataset dataset = Dataset(train_list, test_list) # create a saver saver = tf.train.Saver() #Create log of the results in a text file #textFilesPath = "../specialProblem/" #log = open(textFilesPath + "log.txt", "a") #sys.stdout = log #Create Confusion matrix confusionMatrix = np.zeros((6, 6)) confusionTotal = [0, 0, 0, 0, 0, 0] # Launch the graph with tf.Session() as sess: print('Init variable') sess.run(init) # Load pretrained model # Load weights for AlexNet # load_with_skip('caffenet.npy', sess, ['fc8']) # Skip weights from fc8 # Load weights for VGG vgg.load_weights("vgg16_weights.npz", sess) print('Start training') step = 1 while step < training_iters: batch_xs, batch_ys = dataset.next_batch(batch_size, 'train') sess.run(optimizer, feed_dict={ x: batch_xs, y: batch_ys, keep_var: keep_rate }) # save mid-point models if step % save_step == 0: save_path = saver.save( sess, "model_files/model_" + str(step) + ".ckpt") print("model saved in file: ", save_path) # Display testing status if step % test_step == 0: test_acc = 0. test_count = 0 # confusionMatrix = np.zeros((6, 6)) # confusionTotal = [0, 0, 0, 0, 0, 0] # validPredLabel_count = 0 for _ in range(int(dataset.test_size / batch_size)): batch_tx, batch_ty = dataset.next_batch(batch_size, 'test') acc, pred_label, actual_label = sess.run( (accuracy, tf.argmax(pred, 1), tf.argmax(y, 1)), feed_dict={ x: batch_tx, y: batch_ty, keep_var: 1. }) test_acc += acc test_count += 1 #print(len(pred_label)) # if (max(pred_label) >= 6): # # print(batch_tx) # # print(actual_label) # # print(pred_label) # continue # validPredLabel_count += 1 # i = 0 # while i < len(pred_label): # confusionMatrix[pred_label[i]][actual_label[i]] += 1 # confusionTotal[actual_label[i]] += 1 # i += 1 test_acc /= test_count print( sys.stderr, "{} Iter {}: Testing Accuracy = {:.4f}".format( datetime.now(), step, test_acc)) # print(validPredLabel_count) # for row in range(6): # for col in range(6): # confusionMatrix[row][col] = round((confusionMatrix[row][col] / validPredLabel_count), 2) # print(confusionMatrix) # Display training status if step % display_step == 0: acc = sess.run(accuracy, feed_dict={ x: batch_xs, y: batch_ys, keep_var: 1. }) batch_loss = sess.run(loss, feed_dict={ x: batch_xs, y: batch_ys, keep_var: 1. }) print( sys.stderr, "{} Iter {}: Training Loss = {:.4f}, Accuracy = {:.4f}". format(datetime.now(), step, batch_loss, acc)) step += 1 print("Finish!")
batch_size_tr = 1000 batch_size_te = 200 epochs = 30 n_n = 20 all_costs, all_scores, mi_list = [], [], [] all_scores = [] x_tr, y_tr, x_te, y_te = load_cifar10(path, gpu) for n in range(N): cost, score, mi_sample = [], [], [] if gpu: model = VGG16(10, nn.ReLU()).cuda() else: model = VGG16(10, nn.ReLU()) for epoch in range(epochs): batches_tr = list(model.make_batches(x_tr.shape[0], batch_size_tr)) batches_te = list(model.make_batches(x_te.shape[0], batch_size_te)) for idx_tr, idx_te in zip(batches_tr, batches_te): x_tr_b = x_tr[idx_tr] y_tr_b = y_tr[idx_tr] x_te_b = x_te[idx_te] y_te_b = y_te[idx_te]
shuffle=False, buffer_size=100) # create an reintializable iterator given the dataset structure iterator = Iterator.from_structure(train.data.output_types, train.data.output_shapes) next_batch = iterator.get_next() # 分别给训练数据和验证数据初始化迭代器 training_init_op = iterator.make_initializer(train.data) validation_int_op = iterator.make_initializer(val.data) # 定义占位符 x = tf.placeholder(tf.float32, [batch_size, 224, 224, 3]) y = tf.placeholder(tf.float32, [batch_size, num_classes]) fc_rate = tf.placeholder(tf.float32) # 【问题】不明白这个是干嘛的,dropout不是在网络中已经传入了吗 # 载入网络 vgg16 = VGG16(vgg16_npy_path, False) # 卷积层和池化层的参数不可训练 # vgg16.build(x, num_classes, dropout=rate) vgg16.build(x, num_classes, dropout=fc_rate) # 感觉上面那行代码应该有问题 # 将特征图写入tensorboard, 这里是将第一个feature map写入了tensorboard with tf.variable_scope('feature_map'): conv1_1_feature = vgg16.conv1_1 split_number = conv1_1_feature.get_shape().as_list()[-1] conv1_1 = tf.split(conv1_1_feature, num_or_size_splits=split_number, axis=3) tf.summary.image('conv1', conv1_1[0], max_outputs=4) y_predict = vgg16.fc8 # 计算损失 vgg16.loss(y_predict, y)
# depth = 100 # nb_dense_block = 4 # growth_rate = 12 # nb_filter = 16 # dropout_rate = 0.2 # 0.0 for data augmentation # weight_decay=1E-4 # model = densenet.DenseNet(input_shape=img_dim, depth=depth, nb_dense_block=nb_dense_block, # growth_rate=growth_rate, nb_filter=nb_filter, nb_layers_per_block=-1, # bottleneck=True, reduction=0.0, dropout_rate=dropout_rate, weight_decay=weight_decay, # include_top=True, weights=None, input_tensor=None, # classes=NUM_CLASSES, activation='softmax') ####################################### ########## LOAD MODEL ################# ####################################### model = VGG16(classes=NUM_CLASSES) # model = cnn_model() #model = VGG_16() print("Model created") model.summary() ####################################### ########## OPTIMIZER ################## ####################################### # optimizer = Adam(lr=1e-4) # Using Adam instead of SGD to speed up training optimizer = SGD(lr=learning_rate, decay=0.0, momentum=0.9, nesterov=True) # optimizer = Adam(lr=learning_rate, beta_1=0.9, beta_2=0.999, epsilon=1e-08) model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=['accuracy', f2_beta_keras]) print("Finished compiling")
shuffle=False, num_workers=2) # Dataloader classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck') # %% # I have commented out some lines which I just use during experimentation. import numpy as np #dataiter = iter(trainloader) #images, labels = dataiter.next() from VGG16 import VGG16 # Import network from VGG script #model = VGG16(10, nn.ReLU()).cuda() # Initialize model with 10 classes and relu activation function. model = VGG16(10, nn.ReLU( )) # Note that if you run this scipt on a cpu you must remove the ".cuda()" # from this line. #out, layers = model(images) # Nevermind this part, it is just for prototyping and experimenting. # %% # #mi_mat = np.zeros((1, 17, 17)) #j_mat = np.zeros((1, 17, 17)) # #for i, layer in enumerate(layers, 0): # # mi_mat[0, 0, 0] = model.mutual_information(images, images) # mi_mat[0, 0, i+1] = model.mutual_information(images, layer.cpu()) #