import tflearn
from tflearn.data_utils import shuffle, to_categorical
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.estimator import regression
from tflearn.data_preprocessing import ImagePreprocessing
from tflearn.data_augmentation import ImageAugmentation
from fractal_block import fractal_block
# Data loading and preprocessing
from tflearn.datasets import cifar10
import tensorflow as tf
(X, Y), (X_test, Y_test) = cifar10.load_data()
X, Y = shuffle(X, Y)
Y = to_categorical(Y, 10)
Y_test = to_categorical(Y_test, 10)

# Real-time data preprocessing
img_prep = ImagePreprocessing()
img_prep.add_featurewise_zero_center()
img_prep.add_featurewise_stdnorm()

# Real-time data augmentation
img_aug = ImageAugmentation()
img_aug.add_random_flip_leftright()
# img_aug.add_random_rotation(max_angle=25.)

# Convolutional network building

# from fractal_block import tensor_shape, fractal_block

network = input_data(shape=[None, 32, 32, 3],
                     data_preprocessing=img_prep,
                     data_augmentation=img_aug)
for block, filters in enumerate([64,128,256,512,512]):
    network = fractal_block(network, filters, 2, 2, joined=True)
    network = tflearn.max_pool_2d(network, 2)

network = fully_connected(network, 10, activation='softmax')
network = regression(network, optimizer='adam',
                     loss='categorical_crossentropy',
                     learning_rate=0.001)

# Train using classifier
model = tflearn.DNN(network, tensorboard_verbose=0)
model.fit(X, Y, n_epoch=500, shuffle=True, validation_set=(X_test, Y_test),
          show_metric=True, batch_size=256, run_id='cifar10')

import tflearn
from tflearn.data_utils import shuffle, to_categorical
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.estimator import regression
from tflearn.data_preprocessing import ImagePreprocessing
from tflearn.data_augmentation import ImageAugmentation
from fractal_block import fractal_block
# Data loading and preprocessing
from tflearn.datasets import cifar10
import tensorflow as tf
(X, Y), (X_test, Y_test) = cifar10.load_data()
X, Y = shuffle(X, Y)
Y = to_categorical(Y, 10)
Y_test = to_categorical(Y_test, 10)

# Real-time data preprocessing
img_prep = ImagePreprocessing()
img_prep.add_featurewise_zero_center()
img_prep.add_featurewise_stdnorm()

# Real-time data augmentation
img_aug = ImageAugmentation()
img_aug.add_random_flip_leftright()
# img_aug.add_random_rotation(max_angle=25.)

# Convolutional network building

# from fractal_block import tensor_shape, fractal_block

network = input_data(shape=[None, 32, 32, 3],
                     data_preprocessing=img_prep,
                     data_augmentation=img_aug)
for block, filters in enumerate([64,128,256,512,512]):
    network = fractal_block(network, filters, 2, 2, joined=True)
    network = tflearn.max_pool_2d(network, 2)

network = fully_connected(network, 10, activation='softmax')
network = regression(network, optimizer='adam',
                     loss='categorical_crossentropy',
                     learning_rate=0.001)

# Train using classifier
model = tflearn.DNN(network, tensorboard_verbose=0)
model.fit(X, Y, n_epoch=500, shuffle=True, validation_set=(X_test, Y_test),
          show_metric=True, batch_size=256, run_id='cifar10')

# Data loading and preprocessing # Real-time data preprocessing # Real-time data augmentation # img_aug.add_random_rotation(max_angle=25.) # Convolutional network building # from fractal_block import tensor_shape, fractal_block # Train using classifier