가장 쉬운 딥러닝: 2중 클래스 분류, 다중 클래스 분류, 회귀 [Keras] by 바죠

가장 쉬운 딥러닝: 2중 클래스 분류, 다중 클래스 분류, 회귀
가장 쉬운 딥러닝 pima, iris, housing [deep learning, keras]
Pima 인디언 당뇨병 문제

딥러닝에서는 과적합 (overfitting) 현상이 나타나지 않는 것을 확인하는 것이 가장 중요하다.
Relu, Gaussian initialization을 활용한다. 
Dropout을 활용할 수 있다.

Training program
Prediction program 을 각각 나누어서 프로그램 한 예제는아래와 같다.
먼저 데이터가 아래와 같다고 가정할 경우 pima-indians-diabetes.csv
엑셀 파일과 같은 형식으로 comma separated value 양식일 때를 가정한다.
먼저 training 프로그램을 수행하면 완성된,학습된모델을 디스크에 저장할 수 있다. 아울러 가중치들도 모두 저장할 수 있다.
다음으로 새로운 데이터가 있다고 가정할 때, 물론, 지금의 연습에서는 동일한 데이터를 가정하고 있다.

Prediction 프로그램을 수행하면, 새로운 데이터를 읽어 들이고 이들 데이터에 대해서 예측을 수행할 수 있다.
이 경우, 순수한 예측으로서 정답이 알려져 있지 않다. X 벡터만있고, y 벡터는 없다.
y 벡터가 예측한 값이 될 것이다.
X를 읽어 들이고, 모델모양, 가중치를 모두 읽어 들인 다음, 예측을 수행한다. 이때 인풋은 X 벡터이다.
주어진 X 값에 대해서 y값이 예측된다.

\[ \vec{X} \rightarrow \vec{y} \]


Deep learning 의 핵심 사항은 과적합, overfitting이 일어나지 않는다는 사실이다.  [가장 중요한 사실]
일반적인 가중치를 찾을 수 있다는 것이다 핵심이다.

이중 분류 : sigmoid, 두 가지 상태들 사이의이 상대적인 확률이 정의되어 있다. 이것 아니면 저것인데, 이를 확률적으로 표현한 것이다. 부드럽게 만들어 준다. 
다중 분류 (3종 이상): softmax, sigmoid의 확장형이다. 확률적으로 만들어졌다. 즉, 합이 1이 되는 것 같은 식으로 만들어졌다.
결국, 출력함수가 발산하지 않게 즉, 부드럽게 되도록 만들어준다. 그래서 soft가 붙어있다.

기계학습은 새로운 프로그램을 만드는 것이다.
학습하는 프로그램을 먼저 만든다.
예측할 수 있는 프로그램을 그 다음으로 만든다. 
이러한 순서로 새로운 프로그램 완성이 이루어지게 된다.
새로운 프로그램을 곧바로 만들기 위해서 필요한 정보를 별도로 저장하여 보관한다.

--------------------------------------------------------------------------------------------------------------------
cat pima_training.py
#       Binary classification
#       A training program with keras

import time
from sklearn.model_selection import train_test_split
from sklearn.utils import shuffle
from keras.models import Sequential
from keras.layers import Dense
import numpy as np
import random
import tensorflow as tf
tf.set_random_seed(12)
np.random.seed(34)
random.seed(56)
random.seed(time.time())
start_time=time.clock()
#      fix random seed for reproducibility
#      np.random.seed(7)
#      load pima indians dataset

dataset = np.loadtxt("pima-indians-diabetes.csv", delimiter=",")
#      split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]
X, Y = shuffle(X,Y,random_state=0)
x_train, x_test, y_train, y_test= train_test_split(X,Y, test_size=0.2)
#      create model
model = Sequential()
model.add(Dense(12, input_dim=8, activation='relu'))
for i in range(4):
    model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='
sigmoid'))
model.summary()

#      Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
#      Fit the model
model.fit(x_train, y_train, validation_split=0.10, epochs=100, batch_size=5, verbose=2)
#      evaluate the model
scores = model.evaluate(x_test, y_test)
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
predictions=model.predict(x_test)
rounded=[round(x[0]) for x in predictions]
print(rounded)
if True:
#      serialize model to JSON
    model_json = model.to_json()
    with open("model.json", "w") as json_file:
        json_file.write(model_json)
#      serialize weights to HDF5
    model.save_weights("model.h5")
    print("Saved model to disk")

print((time.clock()-start_time)/60./60.,'hours')

init='normal' 제거됨!



--------------------------------------------------------------------------------------------------------------------
 cat pima_prediction.py
#      Binary classification
#      A prediction program with keras

from keras.models import model_from_json
from sklearn.utils import shuffle
import time
import numpy as np
import random
import tensorflow as tf
tf.set_random_seed(12)
np.random.seed(34)
random.seed(56)
random.seed(time.time())
start_time=time.clock()
#        fix random seed for reproducibility
#        np.random.seed(7)
#        load pima indians dataset

dataset = np.loadtxt("pima-indians-diabetes.csv", delimiter=",")
#        split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]
X, Y = shuffle(X,Y,random_state=0)
#           load json and create model
json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
#           load weights into new model
loaded_model.load_weights("model.h5")
print("Loaded model from disk")
#           evaluate loaded model on test data
loaded_model.compile(loss='mean_squared_error', optimizer='adam')
predicted = loaded_model.predict(X)
print((time.clock()-start_time),'sec')

--------------------------------------------------------------------------------------------------------------------
cat iris_training.py
#       Multi-class classification
#       A training program with keras

from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers import Dense
from keras.utils import np_utils
from sklearn.preprocessing import LabelEncoder
from sklearn.utils import shuffle
from sklearn.metrics import classification_report, confusion_matrix
import tensorflow as tf
import time
import random
import numpy as np
import pandas as pd
tf.set_random_seed(12)
np.random.seed(34)
random.seed(56)
random.seed(time.time())
start_time=time.clock()
df = pd.read_csv('iris.csv',names=["sepal_length", "sepal_width", "petal_length", "petal_width", "species"])
data_set = df.values
X = data_set[:, 0:4].astype(float)
obj_y = data_set[:, 4]
encoder = LabelEncoder()
encoder.fit(obj_y)
Y_encodered = encoder.transform(obj_y)
Y = np_utils.to_categorical(Y_encodered)
X, Y = shuffle(X,Y,random_state=0)
x_train, x_test, y_train, y_test= train_test_split(X,Y, test_size=0.2)
model = Sequential()
model.add(Dense(16, input_dim=4, activation='relu'))
for i in range(3):
    model.add(Dense(10, activation='relu'))
model.add(Dense(3, activation='
softmax'))
model.summary()

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(x_train, y_train, validation_split=0.10, epochs=100, batch_size=5, verbose=2)
scores=model.evaluate(x_test, y_test)
print('\nTest: Loss: {:.4f}'.format(scores[0]))
print('\nTest: Accuracy: {:.4f}'.format(scores[1]))
y_pred=model.predict(x_test)
y_pred=np.argmax(y_pred,axis=1)
y_test=np.argmax(y_test,axis=1)
print(classification_report(y_test,y_pred))
print(confusion_matrix(y_test,y_pred))
if True:
#       serialize model to JSON
    model_json = model.to_json()
    with open("model.json", "w") as json_file:
        json_file.write(model_json)
#       serialize weights to HDF5
    model.save_weights("model.h5")
    print("Saved model to disk")
print((time.clock()-start_time)/60./60.,'hours')


--------------------------------------------------------------------------------------------------------------------
cat iris_prediction.py
#        Multi-class classification
#        A prediction program with keras

from keras.models import model_from_json
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers import Dense
from keras.utils import np_utils
from sklearn.preprocessing import LabelEncoder
from sklearn.utils import shuffle
from sklearn.metrics import classification_report, confusion_matrix
import tensorflow as tf
import time
import random
import numpy as np
import pandas as pd
tf.set_random_seed(12)
np.random.seed(34)
random.seed(56)
random.seed(time.time())
start_time=time.clock()
df = pd.read_csv('iris.csv',names=["sepal_length", "sepal_width", "petal_length", "petal_width", "species"])
data_set = df.values
X = data_set[:, 0:4].astype(float)
obj_y = data_set[:, 4]
encoder = LabelEncoder()
encoder.fit(obj_y)
Y_encodered = encoder.transform(obj_y)
Y = np_utils.to_categorical(Y_encodered)
X, Y = shuffle(X,Y,random_state=0)
x_train, x_test, y_train, y_test= train_test_split(X,Y, test_size=0.2)
#         load json and create model
json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
#         load weights into new model
loaded_model.load_weights("model.h5")
print("Loaded model from disk")
#         evaluate loaded model on test data
loaded_model.compile(loss='mean_squared_error', optimizer='adam')
predicted = loaded_model.predict(X)
print((time.clock()-start_time),'sec')

--------------------------------------------------------------------------------------------------------------------
cat hs_training.py
#       Regression 
#       A training program with keras

import time
from keras.models import Sequential
from keras.layers import Dense
from sklearn.model_selection import train_test_split
import pandas as pd
import numpy as np
import random
import tensorflow as tf
tf.set_random_seed(12)
np.random.seed(34)
random.seed(56)
random.seed(time.time())
start_time=time.clock()
df = pd.read_csv('housing.csv', delim_whitespace=True, header=None)
data_set = df.values
X = data_set[:, 0:13]
Y = data_set[:, 13]
X_train, X_validation, Y_train, Y_validation = train_test_split(X, Y, test_size=0.2)
model = Sequential()
model.add(Dense(30, input_dim=13, activation='relu'))
model.add(Dense(20, activation='relu'))
model.add(Dense(10, activation='relu'))
model.add(Dense(6, activation='relu'))
model.add(Dense(6, activation='relu'))
model.add(Dense(1))
model.summary()
model.compile(loss='mean_squared_error', optimizer='adam')
#model.fit(X_train, Y_train, epochs=200, batch_size=10)
model.fit(X_train, Y_train, validation_split=0.10, epochs=200, batch_size=10, verbose=2)
Y_prediction = model.predict(X_validation).flatten()
for i in range(10):
  real_price = Y_validation[i]
  predicted_price = Y_prediction[i]
  print('Real Price: {:.3f}, Predicted Price: {:.3f}'.format(real_price, predicted_price))
if True:
#        serialize model to JSON
    model_json = model.to_json()
    with open("model.json", "w") as json_file:
        json_file.write(model_json)
#        serialize weights to HDF5
    model.save_weights("model.h5")
    print("Saved model to disk")

print((time.clock()-start_time)/60./60.,'hours')

--------------------------------------------------------------------------------------------------------------------
cat hs_prediction.py
#       Regression 
#       A prediction program with keras

from keras.models import model_from_json
from sklearn.utils import shuffle
import time
from sklearn.model_selection import train_test_split
import pandas as pd
import numpy as np
import random
import tensorflow as tf
tf.set_random_seed(12)
np.random.seed(34)
random.seed(56)
random.seed(time.time())
start_time=time.clock()
df = pd.read_csv('housing.csv', delim_whitespace=True, header=None)
data_set = df.values
X = data_set[:, 0:13]
Y = data_set[:, 13]
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2)
if True:
#         load json and create model
    json_file = open('model.json', 'r')
    loaded_model_json = json_file.read()
    json_file.close()
    loaded_model = model_from_json(loaded_model_json)
#         load weights into new model
    loaded_model.load_weights("model.h5")
    print("Loaded model from disk")
loaded_model.compile(loss='mean_squared_error', optimizer='adam')
Y_prediction = loaded_model.predict(X_test).flatten()
for i in range(10):
     real_price = Y_test[i]
     predicted_price = Y_prediction[i]
     print('Real Price: {:.3f}, Predicted Price: {:.3f}'.format(real_price, predicted_price))
print((time.clock()-start_time)/60./60.,'hours')
--------------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------------
import numpy as np
import random
from sklearn.model_selection import train_test_split 
from keras.models import Sequential
from numpy import mean
from numpy import std
from numpy import dstack
from pandas import read_csv
from matplotlib import pyplot
from keras.utils import to_categorical
from keras.utils.vis_utils import plot_model
from keras.models import Model
from keras.layers import Input
from keras.layers.merge import concatenate
from tensorflow.keras.layers import Conv1D, BatchNormalization, MaxPooling1D, Flatten, Dropout, Dense
import matplotlib.pyplot as plt

x0,y0,grid,ntypes,ndim=dataloading1()
for i in range(ntypes):
    tmq=sum(x0[i,:])
    x0[i,:]=x0[i,:]/tmq*float(ndim)
print('data normalization here')
i=ndim    
#ndim=81*32
print('modified ndim here',i,ndim)
num_classes = ntypes
naugmen=200
y = np.zeros((ntypes*naugmen))
X=np.zeros((ntypes*naugmen,ndim))

i=0
for i0 in range(ntypes):
    for i1 in range(naugmen):
        tmr=(random.random()-0.5)*0.02
        for j in range(ndim):
            X[i,j]=x0[i0,j]+(random.random()-0.5)*0.1+tmr
        y[i]=y0[i0]
        i=i+1    
for i in range(ntypes*naugmen):
    tmq=sum(X[i,:])
    X[i,:]=X[i,:]/tmq*float(ndim)
print('data normalization here again')
        
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=4321)
y_train= to_categorical(y_train)
y_test= to_categorical(y_test)
X_train = X_train.reshape(X_train.shape[0], ndim, 1).astype('float32')
X_test = X_test.reshape(X_test.shape[0], ndim, 1).astype('float32') 

model = Sequential()
model.add(Conv1D(filters=128, kernel_size=5, activation='relu', input_shape=(ndim, 1)))
model.add(BatchNormalization())
model.add(Conv1D(filters=128, kernel_size=3, activation='relu'))
model.add(MaxPooling1D(pool_size=2 ))
model.add(Dropout(0.1))
model.add(Conv1D(filters=128, kernel_size=3, activation='relu'))
model.add(MaxPooling1D(pool_size=2 ))
model.add(Dropout(0.1))
model.add(Flatten())
model.add(Dense(200, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(200, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
model.summary()
plot_model(model, show_shapes=True, to_file='cnn1d_model.png')
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
if False:
    model.fit(X_train, y_train, epochs=500, batch_size=100, verbose=1)
    history = model.fit(X_train, y_train, epochs=500, batch_size=64, verbose=1)
if True:
    history = model.fit(X_train, y_train, batch_size = 100, epochs = 500, validation_split = 0.2, verbose = 0)
    _, accuracy = model.evaluate(X_test, y_test, batch_size=100, verbose=1)
    print(accuracy)
    plt.plot(history.history['accuracy'])
    plt.plot(history.history['val_accuracy'])
    plt.ylabel('Accuracy')
    plt.xlabel('Epoch')
    plt.legend(['training', 'validation'], loc = 'lower right', bbox_to_anchor=(0.6, 0.2), ncol=1, frameon=True, shadow=True, fontsize=14 )
    plt.show()
    plt.plot(history.history['loss'])
    plt.plot(history.history['val_loss'])
#    plt.title('model loss')
    plt.ylabel('Loss')
    plt.xlabel('Epoch')
    plt.legend(['train', 'val'], loc='upper left')
    plt.show()

if False:
    model.save("my_model.h5")
    reconstructed_model=keras.models.load_model("my_model.h5")
if False:
    model.save("my_model.h5")
    print("Saved model to disk")
if False:
    from keras.models import load_model
    model=load_model('my_model.h5')
    model.summary()


import itertools
from sklearn.metrics import confusion_matrix
def plot_confusion_matrix(cm, classes, normalize=False, title='Confusion matrix', cmap=plt.cm.Blues):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting 'normalize=True'.
    """
    plt.figure(figsize = (8,8))
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title,fontsize=20)
    plt.colorbar()
    tick_marks=np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)
    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, cm[i, j], horizontalalignment="center", color="white" if cm[i, j] > thresh else "black")
    plt.tight_layout()
    plt.ylabel('True label',fontsize=20)
    plt.xlabel('Predicted label', fontsize=20)
    plt.show()
y_pred = model.predict(X_test)
y_pred_classes = np.argmax(y_pred, axis = 1)
y_tru = np.argmax(y_test, axis = 1)
confusion_mtx = confusion_matrix(y_tru, y_pred_classes)
plot_confusion_matrix(confusion_mtx, classes = range(num_classes))
cm = confusion_matrix(y_tru, y_pred_classes)
recall = np.diag(cm) / np.sum(cm, axis = 1)
precision = np.diag(cm) / np.sum(cm, axis = 0)
print(np.mean(recall))
print(np.mean(precision))
from sklearn.metrics import precision_score
precision_score(y_tru, y_pred_classes,average='micro')

from sklearn.metrics import precision_score, recall_score, f1_score
p = precision_score(y_tru, y_pred_classes,average='micro')
print(p)
r = recall_score(y_tru, y_pred_classes,average='micro')
print(r)
f1 = f1_score(y_tru, y_pred_classes,average='micro')
print(f1)
from sklearn.metrics import classification_report
target_names = ['class 0', 'class 1', 'class 2', 'class 3', 'class 4', 'class 5', 'class 6', 
               'class 7', 'class 8', 'class 9', 'class 10', 'class 11', 'class 12', 'class 13', 
               'class 14', 'class 15', 'class 16', 'class 17', 'class 18', 'class 19']
print(classification_report(y_tru, y_pred_classes, target_names=target_names))


--------------------------------------------------------------------------------------------------------------------
만들려고 하는 model을 저장할 수 있다.
그리고 언제든지 다시 불러올 수 있고 즉시, 훈련할 수 있다. 물론, prediction 할 수도 있다.

import os
import tensorflow as tf


if True:
    model.save('my_model_tf',save_format='tf')
    print('writing')


if True:
    if os.path.exists('my_model_tf'):
        del model
        model = tf.keras.models.load_model('my_model_tf')
        print('reading')

--------------------------------------------------------------------------------------------------------------------
input_shape = X_train.shape
model.build(input_shape)
model.summary()
if True:
    plot_model(model, show_shapes=True, to_file='cnn1d_model.png')
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
if False:
    model.fit(X_train, y_train, epochs=500, batch_size=100, verbose=1)
if False:
    history = model.fit(X_train, y_train, epochs=500, batch_size=64, verbose=1)
if True:
    if os.path.exists('my_model_tf'):
        del model
        model = tf.keras.models.load_model('my_model_tf')   
        print('reading')
if False:
    history = model.fit(X_train, y_train, batch_size = 100, epochs = 10, validation_split = 0.2, verbose = 1)
    _, accuracy = model.evaluate(X_test, y_test, batch_size=100, verbose=1)
if True:    
    history = model.fit(X_train, y_train, batch_size = 100, epochs = 10, validation_data = (X_test,y_test), verbose = 1)
    _, accuracy = model.evaluate(X_test, y_test, batch_size=100, verbose=1)


--------------------------------------------------------------------------------------------------------------------

if True:
    print("Saving model...")
    train_dataset = tf.data.Dataset.from_tensor_slices(X_train).batch(BATCH_SIZE)
    test_dataset = tf.data.Dataset.from_tensor_slices(X_test).batch(BATCH_SIZE)
    input_shape_set=False
    if not input_shape_set:
        # Note: Why set input shape manually and why here:
        # 1. If I do not set input shape manually: ValueError: Model <main.CVAE object at 0x7f1cac2e7c50> cannot be saved because the input shapes have not been set. Usually, input shapes are automatically determined from calling .fit() or .predict(). To manually set the shapes, call model._set_inputs(inputs).
        # 2. If I set input shape manually BEFORE the first actual train step, I get: RuntimeError: Attempting to capture an EagerTensor without building a function.
        model._set_inputs(train_dataset.__iter__().next())
        input_shape_set = True
        # Note: Why choose tf format: model.save('my_model.h5') will return NotImplementedError: 
        # Saving the model to HDF5 format requires the model to be a Functional model or a Sequential model. 
        # It does not work for subclassed models, because such models are defined via the body of a Python method, 
        # which isn't safely serializable. 
        # Consider saving to the Tensorflow SavedModel format (by setting save_format="tf") or using save_weights.
        model.save('my_model_tf', save_format='tf' , overwrite=True, include_optimizer=True)        
--------------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------
    # evaluate the model
    scores = model.evaluate(X_test, y_test, verbose=0)
    print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
    model.save_weights('my_model_weights.h5')
    print('weights writing')

if True:
    if os.path.exists('my_model_weights.h5'):
        model.load_weights('my_model_weights.h5')
        print('reading weights')

--------------------------------------------------------------------------------------------------------------



------------------------------------------------------------------------------------------------------------------------


------------------------------------------------------------------------------------------------------------------------

from tensorflow.keras.preprocessing.image import ImageDataGenerator

datagen = ImageDataGenerator(
        samplewise_center=True,  # set each sample mean to 0
        rotation_range=10,  # randomly rotate images in the range (degrees, 0 to 180)
        zoom_range = 0.1, # Randomly zoom image 
        width_shift_range=0.1,  # randomly shift images horizontally (fraction of total width)
        height_shift_range=0.1,  # randomly shift images vertically (fraction of total height)
        horizontal_flip=True,  # randomly flip images
        vertical_flip=False) # we don't expect Bo to be upside-down so we will not flip vertically

# load and iterate training dataset
train_it = datagen.flow_from_directory('./COVID/train/', 
                                       target_size=(224, 224), 
                                       color_mode='rgb', 
                                       class_mode='binary', 
                                       batch_size=8)
# load and iterate validation dataset
valid_it = datagen.flow_from_directory('./COVID/test/', 
                                      target_size=(224, 224), 
                                      color_mode='rgb', 
                                      class_mode='binary', 
                                      batch_size=8)

------------------------------------------------------------------------------------------------------------------------



multi-class classification의 경우, 5종 분류인 경우, 폴더 5개를 만들어서 준비한다.

from keras.preprocessing.image import ImageDataGenerator
from keras.utils import np_utils
from keras.models import Sequential
from keras.preprocessing.image import array_to_img, img_to_array, load_img
from keras.layers import *
from keras.callbacks import ModelCheckpoint, EarlyStopping
import os
import pandas as pd
import numpy as np
from sklearn.metrics import accuracy_score

trainDataGen = ImageDataGenerator(rescale=1./255,
rotation_range = 30,  width_shift_range=0.1,   height_shift_range=0.1,  shear_range=0.2,  zoom_range=0.2,  horizontal_flip=False,   vertical_flip=False,   fill_mode='nearest')
trainGenSet = trainDataGen.flow_from_directory( path + 'train',    batch_size=32,    target_size =(28,28), class_mode='categorical')

model = Sequential()
model.add(Conv2D(64, kernel_size=(3,3), padding='same', input_shape=(28,28,3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Conv2D(128, kernel_size=(3,3),padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Conv2D(256, kernel_size=(3,3), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(5, activation='softmax'))
model.summary()
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit_generator(   trainGenSet, steps_per_epoch=20, epochs=200,    validation_data=testGenSet, validation_steps=10)
scores = model.evaluate_generator(testGenSet)
print(scores)



-------------------------

from tensorflow.keras.preprocessing.image import ImageDataGenerator

datagen = ImageDataGenerator(
    rotation_range=10,  # randomly rotate images in the range (degrees, 0 to 180)
    zoom_range=0.1,  # Randomly zoom image
    width_shift_range=0.1,  # randomly shift images horizontally (fraction of total width)
    height_shift_range=0.1,  # randomly shift images vertically (fraction of total height)
    horizontal_flip=True,  # randomly flip images horizontally
    vertical_flip=False, # Don't randomly flip images vertically
)

batch_size = 32
img_iter = datagen.flow(x_train_conv, y_train, batch_size=batch_size)
model.fit(x_train_conv, y_train, epochs=20, verbose=1, validation_data=(x_test_conv, y_test))

# show the first five filters
filters, biases = model.layers[0].get_weights()
print(filters.shape)
plt.subplot(151)
plt.imshow(filters[:,:,0,0], cmap='gray')
plt.subplot(152)
plt.imshow(filters[:,:,0,1], cmap='gray')
plt.subplot(153)
plt.imshow(filters[:,:,0,2], cmap='gray')
plt.subplot(154)
plt.imshow(filters[:,:,0,3], cmap='gray')
plt.subplot(155)
plt.imshow(filters[:,:,0,4], cmap='gray')
plt.show()



------------------------------------------------------------------------------------------------------------------------

ImageDataGenerator  는 폴더을 알아보고 classes를 스스로 정의한다.
정확하게은 메소드가 그 일을 하는데, flow_from_directory

핑백

덧글

댓글 입력 영역

최근 포토로그



MathJax