# 1. 구글 드라이브에 접속,
# 2. ch10_train.zip 파일 복사, 압축해지, ch10_train.zip 파일 삭제
# 3. ch10_test1.zip 파일 복사, 압축해지, ch10_test1.zip 파일 삭제
from google.colab import drive
drive.mount('/content/drive')
%cp /content/drive/My\ Drive/Colab\ Notebooks/영상처리교재\(홍릉\)/ch10_train.zip /content/train.zip
!unzip train.zip
!rm train.zip
#!rm -rf train
%cp /content/drive/My\ Drive/Colab\ Notebooks/영상처리교재\(홍릉\)/ch10_test1.zip /content/test1.zip
!unzip test1.zip
!rm test1.zip
#!rm -rf test1
import numpy as np
# import data
from keras.datasets import mnist
import tensorflow as tf
%matplotlib inline
# load data
(X_train, y_train), (X_test, y_test) = mnist.load_data()
np.random.seed(0)
train_indices = np.random.choice(60000, 50000, replace=False)
valid_indices = [i for i in range(60000) if i not in train_indices]
X_valid, y_valid = X_train[valid_indices,:,:], y_train[valid_indices]
X_train, y_train = X_train[train_indices,:,:], y_train[train_indices]
print(X_train.shape, X_valid.shape, X_test.shape)
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz 11493376/11490434 [==============================] - 0s 0us/step 11501568/11490434 [==============================] - 0s 0us/step (50000, 28, 28) (10000, 28, 28) (10000, 28, 28)
image_size = 28
num_labels = 10
def reformat(dataset, labels):
dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32)
# Map 1 to [0.0, 1.0, 0.0 ...], 2 to [0.0, 0.0, 1.0 ...]
labels = (np.arange(num_labels) == labels[:,None]).astype(np.float32)
return dataset, labels
X_train, y_train = reformat(X_train, y_train)
X_valid, y_valid = reformat(X_valid, y_valid)
X_test, y_test = reformat(X_test, y_test)
print('Training set', X_train.shape, X_train.shape)
print('Validation set', X_valid.shape, X_valid.shape)
print('Test set', X_test.shape, X_test.shape)
Training set (50000, 784) (50000, 784) Validation set (10000, 784) (10000, 784) Test set (10000, 784) (10000, 784)
def accuracy(predictions, labels):
return (100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0])
batch_size = 256 #128
num_hidden_units = 1024
lambda1 = 0.05 #0.005 #0.01
lambda2 = 0.05 #0.005 #0.01
graph = tf.Graph()
with graph.as_default():
# Input data. For the training data, we use a placeholder that will be fed
# at run time with a training minibatch.
tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size))
tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels))
tf_valid_dataset = tf.constant(X_valid)
tf_test_dataset = tf.constant(X_test)
# Variables.
weights1 = tf.Variable(tf.truncated_normal([image_size * image_size, num_hidden_units]))
biases1 = tf.Variable(tf.zeros([num_hidden_units]))
# connect inputs to every hidden unit. Add bias
layer_1_outputs = tf.nn.relu(tf.matmul(tf_train_dataset, weights1) + biases1)
weights2 = tf.Variable(tf.truncated_normal([num_hidden_units, num_labels]))
biases2 = tf.Variable(tf.zeros([num_labels]))
# Training computation.
logits = tf.matmul(layer_1_outputs, weights2) + biases2
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits) + \
lambda1*tf.nn.l2_loss(weights1) + lambda2*tf.nn.l2_loss(weights2))
# Optimizer.
optimizer = tf.train.GradientDescentOptimizer(0.003).minimize(loss)
# Predictions for the training, validation, and test data.
train_prediction = tf.nn.softmax(logits)
layer_1_outputs = tf.nn.relu(tf.matmul(tf_valid_dataset, weights1) + biases1)
valid_prediction = tf.nn.softmax(tf.matmul(layer_1_outputs, weights2) + biases2)
layer_1_outputs = tf.nn.relu(tf.matmul(tf_test_dataset, weights1) + biases1)
test_prediction = tf.nn.softmax(tf.matmul(layer_1_outputs, weights2) + biases2)
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-14-8eac165a99c8> in <module>() 9 # Input data. For the training data, we use a placeholder that will be fed 10 # at run time with a training minibatch. ---> 11 tf_train_dataset = tf.placeholder(tf.float32, shape=(batch_size, image_size * image_size)) 12 tf_train_labels = tf.placeholder(tf.float32, shape=(batch_size, num_labels)) 13 tf_valid_dataset = tf.constant(X_valid) AttributeError: module 'tensorflow' has no attribute 'placeholder'
import keras
from keras.models import Sequential
from keras.layers import Dense, Flatten, Dropout
from keras.layers.convolutional import Conv2D, MaxPooling2D
from tensorflow.keras.utils import to_categorical
# import data
from keras.datasets import mnist
# load data
(X_train, y_train), (X_test, y_test) = mnist.load_data()
print(X_train.shape, X_test.shape)
# reshape to be [samples][pixels][width][height]
X_train = X_train.reshape(X_train.shape[0], 28, 28, 1).astype('float32')
X_test = X_test.reshape(X_test.shape[0], 28, 28, 1).astype('float32')
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
num_classes = y_test.shape[1] # number of categories
def convolutional_model():
# create model
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
input_shape=(28,28,1)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
# compile model
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
return model
# build the model
model = convolutional_model()
model.summary()
# fit the model
model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=10, batch_size=200, verbose=2)
# evaluate the model
scores = model.evaluate(X_test, y_test, verbose=0)
print("Accuracy: {} \n Error: {}".format(scores[1], 100-scores[1]*100))
(60000, 28, 28) (10000, 28, 28) Model: "sequential" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d (Conv2D) (None, 26, 26, 32) 320 conv2d_1 (Conv2D) (None, 24, 24, 64) 18496 max_pooling2d (MaxPooling2D (None, 12, 12, 64) 0 ) dropout (Dropout) (None, 12, 12, 64) 0 flatten (Flatten) (None, 9216) 0 dense (Dense) (None, 128) 1179776 dropout_1 (Dropout) (None, 128) 0 dense_1 (Dense) (None, 10) 1290 ================================================================= Total params: 1,199,882 Trainable params: 1,199,882 Non-trainable params: 0 _________________________________________________________________ Epoch 1/10 300/300 - 153s - loss: 0.9503 - accuracy: 0.8554 - val_loss: 0.0758 - val_accuracy: 0.9781 - 153s/epoch - 510ms/step Epoch 2/10 300/300 - 149s - loss: 0.1546 - accuracy: 0.9553 - val_loss: 0.0597 - val_accuracy: 0.9837 - 149s/epoch - 496ms/step Epoch 3/10 300/300 - 149s - loss: 0.1197 - accuracy: 0.9645 - val_loss: 0.0523 - val_accuracy: 0.9839 - 149s/epoch - 496ms/step Epoch 4/10 300/300 - 146s - loss: 0.0988 - accuracy: 0.9709 - val_loss: 0.0426 - val_accuracy: 0.9878 - 146s/epoch - 486ms/step Epoch 5/10
--------------------------------------------------------------------------- KeyboardInterrupt Traceback (most recent call last) <ipython-input-17-90ce8869ce0e> in <module>() 45 46 # fit the model ---> 47 model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=10, batch_size=200, verbose=2) 48 49 # evaluate the model /usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs) 62 filtered_tb = None 63 try: ---> 64 return fn(*args, **kwargs) 65 except Exception as e: # pylint: disable=broad-except 66 filtered_tb = _process_traceback_frames(e.__traceback__) /usr/local/lib/python3.7/dist-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing) 1382 _r=1): 1383 callbacks.on_train_batch_begin(step) -> 1384 tmp_logs = self.train_function(iterator) 1385 if data_handler.should_sync: 1386 context.async_wait() /usr/local/lib/python3.7/dist-packages/tensorflow/python/util/traceback_utils.py in error_handler(*args, **kwargs) 148 filtered_tb = None 149 try: --> 150 return fn(*args, **kwargs) 151 except Exception as e: 152 filtered_tb = _process_traceback_frames(e.__traceback__) /usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/def_function.py in __call__(self, *args, **kwds) 913 914 with OptionalXlaContext(self._jit_compile): --> 915 result = self._call(*args, **kwds) 916 917 new_tracing_count = self.experimental_get_tracing_count() /usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/def_function.py in _call(self, *args, **kwds) 945 # In this case we have created variables on the first call, so we run the 946 # defunned version which is guaranteed to never create variables. --> 947 return self._stateless_fn(*args, **kwds) # pylint: disable=not-callable 948 elif self._stateful_fn is not None: 949 # Release the lock early so that multiple threads can perform the call /usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/function.py in __call__(self, *args, **kwargs) 2955 filtered_flat_args) = self._maybe_define_function(args, kwargs) 2956 return graph_function._call_flat( -> 2957 filtered_flat_args, captured_inputs=graph_function.captured_inputs) # pylint: disable=protected-access 2958 2959 @property /usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/function.py in _call_flat(self, args, captured_inputs, cancellation_manager) 1852 # No tape is watching; skip to running the function. 1853 return self._build_call_outputs(self._inference_function.call( -> 1854 ctx, args, cancellation_manager=cancellation_manager)) 1855 forward_backward = self._select_forward_and_backward_functions( 1856 args, /usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/function.py in call(self, ctx, args, cancellation_manager) 502 inputs=args, 503 attrs=attrs, --> 504 ctx=ctx) 505 else: 506 outputs = execute.execute_with_cancellation( /usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name) 53 ctx.ensure_initialized() 54 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name, ---> 55 inputs, attrs, num_outputs) 56 except core._NotOkStatusException as e: 57 if name is not None: KeyboardInterrupt:
import os
import numpy as np
import cv2
from random import shuffle
from tqdm import tqdm # percentage bar for tasks.
# download the cats/dogs images compressed train and test datasets from here: https://www.kaggle.com/c/dogs-vs-cats/data
# unzip the train.zip images under the train folder and test.zip images under the test folder
train = './train'
test = './test'
lr = 1e-6 # learning rate
image_size = 50 # all the images will be resized to squaure images with this dimension
model_name = 'cats_dogs-{}-{}.model'.format(lr, 'conv2')
model_name
'cats_dogs-1e-06-conv2.model'
def label_image(image):
word_label = image.split('.')[-3]
if word_label == 'cat': return 0
elif word_label == 'dog': return 1
def create_training_data():
training_data = []
for image in tqdm(os.listdir(train)):
path = os.path.join(train, image)
label = label_image(image)
image = cv2.imread(path)
image = cv2.resize(image, (image_size, image_size))
training_data.append([np.array(image),np.array(label)])
shuffle(training_data)
np.save('train_data.npy', training_data)
return training_data
def create_test_data():
testing_data = []
for image in tqdm(os.listdir(test)):
path = os.path.join(test, image)
image = cv2.imread(path)
image = cv2.resize(image, (image_size, image_size))
testing_data.append(np.array(image))
shuffle(testing_data)
np.save('test_data.npy', testing_data)
return testing_data
train_data = create_training_data()
len(train_data)
25000
from keras.applications.vgg16 import VGG16
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.utils import to_categorical
train = train_data[:-5000] # 20k images for training
valid = train_data[-5000:] # 5k images for validation
X_train = np.array([i[0] for i in train]).reshape(-1,image_size,image_size,3)
y_train = [i[1] for i in train]
y_train = to_categorical(y_train)
print(X_train.shape, y_train.shape)
X_valid = np.array([i[0] for i in valid]).reshape(-1,image_size,image_size,3)
y_valid = [i[1] for i in valid]
y_valid = to_categorical(y_valid) # to one-hot encoding
num_classes = y_valid.shape[1] # number of categories
model = VGG16(weights=None, input_shape=(image_size,image_size,3), classes=num_classes) # train VGG16 model from scratch
model.compile(Adam(lr=lr), "categorical_crossentropy", metrics=["accuracy"]) # "adam"
model.summary()
# fit the model, it's going take a long time if not run on GPU
model.fit(X_train, y_train, validation_data=(X_valid, y_valid), epochs=20, batch_size=256, verbose=2)
# evaluate the model
scores = model.evaluate(X_valid, y_valid, verbose=0)
print("Accuracy: {} \n Error: {}".format(scores[1], 100-scores[1]*100))
(20000, 50, 50, 3) (20000, 2) Model: "vgg16" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= input_2 (InputLayer) [(None, 50, 50, 3)] 0 block1_conv1 (Conv2D) (None, 50, 50, 64) 1792 block1_conv2 (Conv2D) (None, 50, 50, 64) 36928 block1_pool (MaxPooling2D) (None, 25, 25, 64) 0 block2_conv1 (Conv2D) (None, 25, 25, 128) 73856 block2_conv2 (Conv2D) (None, 25, 25, 128) 147584 block2_pool (MaxPooling2D) (None, 12, 12, 128) 0 block3_conv1 (Conv2D) (None, 12, 12, 256) 295168 block3_conv2 (Conv2D) (None, 12, 12, 256) 590080 block3_conv3 (Conv2D) (None, 12, 12, 256) 590080 block3_pool (MaxPooling2D) (None, 6, 6, 256) 0 block4_conv1 (Conv2D) (None, 6, 6, 512) 1180160 block4_conv2 (Conv2D) (None, 6, 6, 512) 2359808 block4_conv3 (Conv2D) (None, 6, 6, 512) 2359808 block4_pool (MaxPooling2D) (None, 3, 3, 512) 0 block5_conv1 (Conv2D) (None, 3, 3, 512) 2359808 block5_conv2 (Conv2D) (None, 3, 3, 512) 2359808 block5_conv3 (Conv2D) (None, 3, 3, 512) 2359808 block5_pool (MaxPooling2D) (None, 1, 1, 512) 0 flatten (Flatten) (None, 512) 0 fc1 (Dense) (None, 4096) 2101248 fc2 (Dense) (None, 4096) 16781312 predictions (Dense) (None, 2) 8194 ================================================================= Total params: 33,605,442 Trainable params: 33,605,442 Non-trainable params: 0 _________________________________________________________________
/usr/local/lib/python3.7/dist-packages/keras/optimizer_v2/adam.py:105: UserWarning: The `lr` argument is deprecated, use `learning_rate` instead. super(Adam, self).__init__(name, **kwargs)
Epoch 1/20