――I will touch Python after a long time. I'm addicted to it now, so I'd like to post only the content and execution results and update it as it progresses. --Finally, previously, I tried to move the SSD model with copy and paste, but I want to be able to make things within myself. .. ――This time, the VGG16 model will be applied in the deep learning model, but there are not many that are carefully explained about what was implemented using TensorFlow, and the SSD model also has the concept of this VGG16 model. Since it is included, I tried it as a stepping stone. --Initially, it was implemented using TF-Slim, but it was redundant at this time because an error occurred when creating the model. I tried to implement it as it is.
-Learning data and Learning program are used in this area.
http://tensorflow.classcat.com/2016/10/18/tensorflow-vgg-model/
network.py
#!/usr/local/bin/python
# -*- coding: utf-8 -*-
import numpy as np
import tensorflow as tf
def vgg16(image, keep_prob):
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
def conv2d(conv, weight):
return tf.nn.conv2d(conv, weight, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(conv):
return tf.nn.max_pool(conv, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
with tf.name_scope('conv1_1') as scope:
weight = weight_variable([3, 3, 3, 64])
bias = bias_variable([64])
conv = conv2d(image, weight)
out = tf.nn.bias_add(conv, bias)
conv1_1 = tf.nn.relu(out, name=scope)
with tf.name_scope('conv1_2') as scope:
weight = weight_variable([3, 3, 64, 64])
bias = bias_variable([64])
conv = conv2d(conv1_1, weight)
out = tf.nn.bias_add(conv, bias)
conv1_2 = tf.nn.relu(out, name=scope)
with tf.name_scope('pool1') as scope:
pool1 = max_pool_2x2(conv1_2)
with tf.name_scope('conv2_1') as scope:
weight = weight_variable([3, 3, 64, 128])
bias = bias_variable([128])
conv = conv2d(pool1, weight)
out = tf.nn.bias_add(conv, bias)
conv2_1 = tf.nn.relu(out, name=scope)
with tf.name_scope('conv2_2') as scope:
weight = weight_variable([3, 3, 128, 128])
bias = bias_variable([128])
conv = conv2d(conv2_1, weight)
out = tf.nn.bias_add(conv, bias)
conv2_2 = tf.nn.relu(out, name=scope)
with tf.name_scope('pool2') as scope:
pool2 = max_pool_2x2(conv2_2)
with tf.name_scope('conv3_1') as scope:
weight = weight_variable([3, 3, 128, 256])
bias = bias_variable([256])
conv = conv2d(pool2, weight)
out = tf.nn.bias_add(conv, bias)
conv3_1 = tf.nn.relu(out, name=scope)
with tf.name_scope('conv3_2') as scope:
weight = weight_variable([3, 3, 256, 256])
bias = bias_variable([256])
conv = conv2d(conv3_1, weight)
out = tf.nn.bias_add(conv, bias)
conv3_2 = tf.nn.relu(out, name=scope)
with tf.name_scope('conv3_3') as scope:
weight = weight_variable([3, 3, 256, 256])
bias = bias_variable([256])
conv = conv2d(conv3_2, weight)
out = tf.nn.bias_add(conv, bias)
conv3_3 = tf.nn.relu(out, name=scope)
with tf.name_scope('pool3') as scope:
pool3 = max_pool_2x2(conv3_3)
with tf.name_scope('conv4_1') as scope:
weight = weight_variable([3, 3, 256, 512])
bias = bias_variable([512])
conv = conv2d(pool3, weight)
out = tf.nn.bias_add(conv, bias)
conv4_1 = tf.nn.relu(out, name=scope)
with tf.name_scope('conv4_2') as scope:
weight = weight_variable([3, 3, 512, 512])
bias = bias_variable([512])
conv = conv2d(conv4_1, weight)
out = tf.nn.bias_add(conv, bias)
conv4_2 = tf.nn.relu(out, name=scope)
with tf.name_scope('conv4_3') as scope:
weight = weight_variable([3, 3, 512, 512])
bias = bias_variable([512])
conv = conv2d(conv4_2, weight)
out = tf.nn.bias_add(conv, bias)
conv4_3 = tf.nn.relu(out, name=scope)
with tf.name_scope('pool4') as scope:
pool4 = max_pool_2x2(conv4_3)
with tf.name_scope('conv5_1') as scope:
weight = weight_variable([3, 3, 512, 512])
bias = bias_variable([512])
conv = conv2d(pool4, weight)
out = tf.nn.bias_add(conv, bias)
conv5_1 = tf.nn.relu(out, name=scope)
with tf.name_scope('conv5_2') as scope:
weight = weight_variable([3, 3, 512, 512])
bias = bias_variable([512])
conv = conv2d(conv5_1, weight)
out = tf.nn.bias_add(conv, bias)
conv5_2 = tf.nn.relu(out, name=scope)
with tf.name_scope('conv5_3') as scope:
weight = weight_variable([3, 3, 512, 512])
bias = bias_variable([512])
conv = conv2d(conv5_2, weight)
out = tf.nn.bias_add(conv, bias)
conv5_3 = tf.nn.relu(out, name=scope)
with tf.name_scope('pool5') as scope:
pool5 = max_pool_2x2(conv5_3)
with tf.name_scope('fc6') as scope:
shape = int(np.prod(pool5.get_shape()[1:]))
weight = weight_variable([shape, 4096])
bias = bias_variable([4096])
pool5_flat = tf.reshape(pool5, [-1, shape])
fc6 = tf.nn.relu(tf.nn.bias_add(tf.matmul(pool5_flat, weight), bias))
# fc6_drop = tf.nn.dropout(fc6, keep_prob)
with tf.name_scope('fc7') as scope:
weight = weight_variable([4096, 4096])
bias = bias_variable([4096])
fc7 = tf.nn.relu(tf.nn.bias_add(tf.matmul(fc6, weight), bias))
# fc7_drop = tf.nn.dropout(fc7, keep_prob)
with tf.name_scope('fc8') as scope:
weight = weight_variable([4096, 3])
bias = bias_variable([3])
fc8 = tf.nn.bias_add(tf.matmul(fc7, weight), bias)
with tf.name_scope('softmax') as scope:
probs = tf.nn.softmax(fc8)
return probs
2017-08-01 17:39:33.518478: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2017-08-01 17:39:33.518513: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-08-01 17:39:33.518523: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2017-08-01 17:39:33.518532: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
step 0, training accuracy 0.588235
step 1, training accuracy 0.588235
step 2, training accuracy 0.588235
step 3, training accuracy 0.588235
step 4, training accuracy 0.588235
step 5, training accuracy 0.588235
step 6, training accuracy 0.588235
step 7, training accuracy 0.588235
--The image size of the VGG model is 224x224 by default, which seems to be at least 48x48, so I set it to 48 or 224. → No change.
――I tried to reduce the number of layers. → No change.
――I changed the learning data. → No change.
――The notation is the same as it is now, and I changed it to the normal CNN layer. → Learning was done normally.
(゚ д ゚)
Recommended Posts