<jupyter_start><jupyter_text>Load and Preprocess the dataset<jupyter_code># Convert to grayscale
def converter(x):
    #x has shape (batch, width, height, channels)
    return (0.2989 * x[:,:,:,:1]) + (0.5870 * x[:,:,:,1:2]) + (0.1140 * x[:,:,:,-1:])
# load the dataset
(x_train, _), (x_test, _) = cifar10.load_data()

xtrain = converter(x_train)
xtest = converter(x_test)

xtrain = xtrain.astype('uint8')
xtest = xtest.astype('uint8')

# Canny Edge Detection
xtrain_temp=[]
for i in range(x_train.shape[0]):
  xtrain_temp.append(cv2.Canny(xtrain[i],100,200))

xtest_temp=[]
for i in range(x_test.shape[0]):
  xtest_temp.append(cv2.Canny(xtest[i],100,200))

xtrain = np.asarray(xtrain_temp)
xtest = np.asarray(xtest_temp)

xtrain = xtrain.astype('float32') / 255.
xtest = xtest.astype('float32') / 255.

xtrain = xtrain.reshape(len(xtrain), np.prod(xtrain.shape[1:]))
xtest = xtest.reshape(len(xtest), np.prod(xtest.shape[1:]))<jupyter_output>Downloading data from https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz
170500096/170498071 [==============================] - 12s 0us/step<jupyter_text>Define the model<jupyter_code>input_layer = xtrain.shape[1]
hid_layer1 = 576
hid_layer2 = 256
hid_layer3 = 64
hid_layer4 = 10
hid_layer5 = hid_layer3
hid_layer6 = hid_layer2
hid_layer7 = hid_layer1
output_layer = input_layer
print(xtrain.shape)
print(xtest.shape)
input_img = Input(shape=(input_layer,))
# network architecture
# vanilla autoencoder with fully-connected layer
# ENCODER
x = Dense(units = hid_layer1, activation='relu')(input_img)
# x = BatchNormalization()(x)
x = Dense(units = hid_layer2, activation='relu')(x)
# x = BatchNormalization()(x)
x = Dense(units = hid_layer3, activation='relu')(x)
# x = BatchNormalization()(x)

encoded = Dense(units = hid_layer4, activation='relu',kernel_regularizer = l2(3e-5), activity_regularizer = l1(10e-9))(x)

# DECODER
x = Dense(units = hid_layer5, activation = 'relu')(encoded)
# x = BatchNormalization()(x)
x = Dense(units = hid_layer6, activation='relu')(x)
# x = BatchNormalization()(x)
x = Dense(units = hid_layer7, activation='relu')(x)
# x = BatchNormalization()(x)

decoded = Dense(units = output_layer, activation='sigmoid')(x)

autoencoder = Model(input_img, decoded)
encoder = Model(input_img, encoded)
autoencoder.compile(optimizer= Adam(lr = 1e-5), loss='binary_crossentropy')
autoencoder.summary()<jupyter_output>_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 1024)              0         
_________________________________________________________________
dense_1 (Dense)              (None, 576)               590400    
_________________________________________________________________
dense_2 (Dense)              (None, 256)               147712    
_________________________________________________________________
dense_3 (Dense)              (None, 64)                16448     
_________________________________________________________________
dense_4 (Dense)              (None, 10)                650       
_________________________________________________________________
dense_5 (Dense)              (None, 64)                704       
_________________________________________________________________
dense_6 (D[...]<jupyter_text>Train Model<jupyter_code># train the model
history = autoencoder.fit(xtrain, xtrain,
                          epochs=50,
                          batch_size=128,
                          shuffle=True,
                          validation_data=(xtest, xtest))
# list all data in history
print(history.history.keys())
# summarize history for loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
from google.colab import drive
drive.mount('/content/gdrive')

autoencoder.save('/content/gdrive/My Drive/autoencoder_edge.h5')<jupyter_output>Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth?client_id=947318989803-6bn6qk8qdgf4n4g3pfee6491hc0brc4i.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=email%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdocs.test%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.photos.readonly%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fpeopleapi.readonly&response_type=code

Enter your authorization code:
··········
Mounted at /content/gdrive<jupyter_text>(Optional) Load Pre-Trained Model<jupyter_code>from keras.models import load_model

autoencoder = load_model('/content/gdrive/My Drive/autoencoder_edge.h5')
enocder = Model(inputs = autoencoder.input, outputs = autoencoder.get_layer(index = 4).output)<jupyter_output><empty_output><jupyter_text>Reconstruction Results<jupyter_code># show the result at the decoder output
decoded_imgs = autoencoder.predict(xtest)

counter = 0
n = 10
plt.figure(figsize=(20, 4))
for i in range(n):
    # display original
    ax = plt.subplot(2, n, i + 1)
    plt.imshow(xtest[counter + i].reshape(32, 32),cmap = 'gray')
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

    # display reconstruction
    ax = plt.subplot(2, n, i + n + 1)
    plt.imshow(decoded_imgs[counter + i].reshape(32, 32),cmap = 'gray')
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
plt.show()
abc = decoded_imgs[8] >= 0.4
plt.imshow(abc.reshape(32,32))
# show the representation at the bottleneck
encoded_imgs = encoder.predict(xtest)

n = 10
plt.figure(figsize=(20, 8))
for i in range(n):
    ax = plt.subplot(2, 5, i + 1)
    plt.plot(encoded_imgs[counter + i])
    # plt.gray()
    ax.get_xaxis().set_visible(True)
    ax.get_yaxis().set_visible(True)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14878661087866107, 'fraction_numerical': 0.04334728033472803, 'mean_word_length': 4.768339768339768, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 19, 'https://': 2, 'lorem ipsum': 0, 'www.': 5, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '31340', 'license': 'MIT', 'n_tokens_mistral': 2232, 'n_tokens_neox': 2074, 'n_words': 354, 'path': 'notebooks/Model_5_Vanilla_AE_EDGE.ipynb', 'repo_name': 'hcxr1/p-tamarix', 'size': '5975'}