from tensorflow.keras.datasets import cifar10
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Dense, Conv2D, LSTM
from tensorflow.keras.layers import MaxPooling2D, Flatten, Dropout
from sklearn.preprocessing import MinMaxScaler
import matplotlib.pyplot as plt
import numpy as np

(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x_predict=x_test[:10, :, :, :]

x_train=x_train.astype('float32')/255.
x_test=x_test.astype('float32')/255.
x_predict=x_predict.astype('float32')/255.

y_train=to_categorical(y_train) 
y_test=to_categorical(y_test)

model=Sequential()
model.add(Conv2D(64, (3,3), input_shape=(32,32,3)))
model.add(Dropout(0.1))
model.add(Conv2D(64, (3,3)))
model.add(Dropout(0.2))
model.add(MaxPooling2D(pool_size=2))
model.add(Conv2D(128, (3,3)))
model.add(Dropout(0.3))
model.add(Conv2D(256, (3,3)))
model.add(Dropout(0.4))
model.add(MaxPooling2D(pool_size=2))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(512, activation='relu'))
model.add(Dense(10, activation='softmax')) 

model.summary()

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

from tensorflow.keras.callbacks import EarlyStopping, TensorBoard
es=EarlyStopping(monitor='loss', patience=10, mode='auto')
# to_hist=TensorBoard(log_dir='graph', histogram_freq=0, write_graph=True, write_images=True)

model.fit(x_train, y_train, epochs=1000, batch_size=512, verbose=1, validation_split=0.2, callbacks=[es])

#4. 평가, 예측
loss, accuracy=model.evaluate(x_test, y_test, batch_size=512)

print('loss : ', loss)
print('accuracy : ', accuracy)

y_predict=model.predict(x_predict)
y_predict=np.argmax(y_predict, axis=1) #One hot encoding의 decoding은 numpy의 argmax를 사용한다.
y_actually=np.argmax(y_test[:10, :], axis=1)
print('실제값 : ', y_actually)
print('예측값 : ', y_predict)

'''
cifar10 CNN
loss :  41.116790771484375
accuracy :  0.5354999899864197
실제값 :  [3 8 8 0 6 6 1 6 3 1]        
예측값 :  [6 8 2 0 6 6 1 6 3 8] 
'''
da42a296696b07bbcddd47d24b918944ffdb1e3c
<mask token>

<mask token>
model.add(Conv2D(64, (3, 3), input_shape=(32, 32, 3)))
model.add(Dropout(0.1))
model.add(Conv2D(64, (3, 3)))
model.add(Dropout(0.2))
model.add(MaxPooling2D(pool_size=2))
model.add(Conv2D(128, (3, 3)))
model.add(Dropout(0.3))
model.add(Conv2D(256, (3, 3)))
model.add(Dropout(0.4))
model.add(MaxPooling2D(pool_size=2))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(512, activation='relu'))
model.add(Dense(10, activation='softmax'))
model.summary()
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=[
    'accuracy'])
<mask token>
model.fit(x_train, y_train, epochs=1000, batch_size=512, verbose=1,
    validation_split=0.2, callbacks=[es])
<mask token>
print('loss : ', loss)
print('accuracy : ', accuracy)
<mask token>
print('실제값 : ', y_actually)
print('예측값 : ', y_predict)
<mask token>

<mask token>
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x_predict = x_test[:10, :, :, :]
x_train = x_train.astype('float32') / 255.0
x_test = x_test.astype('float32') / 255.0
x_predict = x_predict.astype('float32') / 255.0
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
model = Sequential()
model.add(Conv2D(64, (3, 3), input_shape=(32, 32, 3)))
model.add(Dropout(0.1))
model.add(Conv2D(64, (3, 3)))
model.add(Dropout(0.2))
model.add(MaxPooling2D(pool_size=2))
model.add(Conv2D(128, (3, 3)))
model.add(Dropout(0.3))
model.add(Conv2D(256, (3, 3)))
model.add(Dropout(0.4))
model.add(MaxPooling2D(pool_size=2))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(512, activation='relu'))
model.add(Dense(10, activation='softmax'))
model.summary()
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=[
    'accuracy'])
<mask token>
es = EarlyStopping(monitor='loss', patience=10, mode='auto')
model.fit(x_train, y_train, epochs=1000, batch_size=512, verbose=1,
    validation_split=0.2, callbacks=[es])
loss, accuracy = model.evaluate(x_test, y_test, batch_size=512)
print('loss : ', loss)
print('accuracy : ', accuracy)
y_predict = model.predict(x_predict)
y_predict = np.argmax(y_predict, axis=1)
y_actually = np.argmax(y_test[:10, :], axis=1)
print('실제값 : ', y_actually)
print('예측값 : ', y_predict)
<mask token>

from tensorflow.keras.datasets import cifar10
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Dense, Conv2D, LSTM
from tensorflow.keras.layers import MaxPooling2D, Flatten, Dropout
from sklearn.preprocessing import MinMaxScaler
import matplotlib.pyplot as plt
import numpy as np
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x_predict = x_test[:10, :, :, :]
x_train = x_train.astype('float32') / 255.0
x_test = x_test.astype('float32') / 255.0
x_predict = x_predict.astype('float32') / 255.0
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
model = Sequential()
model.add(Conv2D(64, (3, 3), input_shape=(32, 32, 3)))
model.add(Dropout(0.1))
model.add(Conv2D(64, (3, 3)))
model.add(Dropout(0.2))
model.add(MaxPooling2D(pool_size=2))
model.add(Conv2D(128, (3, 3)))
model.add(Dropout(0.3))
model.add(Conv2D(256, (3, 3)))
model.add(Dropout(0.4))
model.add(MaxPooling2D(pool_size=2))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(512, activation='relu'))
model.add(Dense(10, activation='softmax'))
model.summary()
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=[
    'accuracy'])
from tensorflow.keras.callbacks import EarlyStopping, TensorBoard
es = EarlyStopping(monitor='loss', patience=10, mode='auto')
model.fit(x_train, y_train, epochs=1000, batch_size=512, verbose=1,
    validation_split=0.2, callbacks=[es])
loss, accuracy = model.evaluate(x_test, y_test, batch_size=512)
print('loss : ', loss)
print('accuracy : ', accuracy)
y_predict = model.predict(x_predict)
y_predict = np.argmax(y_predict, axis=1)
y_actually = np.argmax(y_test[:10, :], axis=1)
print('실제값 : ', y_actually)
print('예측값 : ', y_predict)
<mask token>

from tensorflow.keras.datasets import cifar10
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Dense, Conv2D, LSTM
from tensorflow.keras.layers import MaxPooling2D, Flatten, Dropout
from sklearn.preprocessing import MinMaxScaler
import matplotlib.pyplot as plt
import numpy as np

(x_train, y_train), (x_test, y_test) = cifar10.load_data()
x_predict=x_test[:10, :, :, :]

x_train=x_train.astype('float32')/255.
x_test=x_test.astype('float32')/255.
x_predict=x_predict.astype('float32')/255.

y_train=to_categorical(y_train) 
y_test=to_categorical(y_test)

model=Sequential()
model.add(Conv2D(64, (3,3), input_shape=(32,32,3)))
model.add(Dropout(0.1))
model.add(Conv2D(64, (3,3)))
model.add(Dropout(0.2))
model.add(MaxPooling2D(pool_size=2))
model.add(Conv2D(128, (3,3)))
model.add(Dropout(0.3))
model.add(Conv2D(256, (3,3)))
model.add(Dropout(0.4))
model.add(MaxPooling2D(pool_size=2))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(512, activation='relu'))
model.add(Dense(10, activation='softmax')) 

model.summary()

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

from tensorflow.keras.callbacks import EarlyStopping, TensorBoard
es=EarlyStopping(monitor='loss', patience=10, mode='auto')
# to_hist=TensorBoard(log_dir='graph', histogram_freq=0, write_graph=True, write_images=True)

model.fit(x_train, y_train, epochs=1000, batch_size=512, verbose=1, validation_split=0.2, callbacks=[es])

#4. 평가, 예측
loss, accuracy=model.evaluate(x_test, y_test, batch_size=512)

print('loss : ', loss)
print('accuracy : ', accuracy)

y_predict=model.predict(x_predict)
y_predict=np.argmax(y_predict, axis=1) #One hot encoding의 decoding은 numpy의 argmax를 사용한다.
y_actually=np.argmax(y_test[:10, :], axis=1)
print('실제값 : ', y_actually)
print('예측값 : ', y_predict)

'''
cifar10 CNN
loss :  41.116790771484375
accuracy :  0.5354999899864197
실제값 :  [3 8 8 0 6 6 1 6 3 1]        
예측값 :  [6 8 2 0 6 6 1 6 3 8] 
'''