<jupyter_start><jupyter_text>Keras, Regression, and CNNs    Removing the fully-connected softmax classifier layer typically used for classificationReplacing it with a fully-connected layer with a single node along with a linear activation function.Training the model with a continuous value prediction loss function such as mean squared error, mean absolute error, mean absolute percentage error, etc. https://github.com/emanhamed/Houses-datasethttps://arxiv.org/pdf/1609.08399.pdf<jupyter_code>import cv2
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
import os
import glob
import matplotlib.pyplot as plt
from tensorflow.keras.optimizers import Adam
inputPath = "D:/college-lessons/MA/FIU/AI-workshop/dataset/Houses-dataset-master/Houses Dataset/HousesInfo.txt"
datasetPath = "D:/college-lessons/MA/FIU/AI-workshop/dataset/Houses-dataset-master/Houses Dataset"
cols = ["bedrooms", "bathrooms", "area", "zipcode", "price"]
df = pd.read_csv(inputPath, sep=" ", header=None, names=cols)


zipcodes, counts = np.unique(df["zipcode"], return_counts=True)

# loop over each of the unique zip codes and their corresponding
# count
for (zipcode, count) in zip(zipcodes, counts):
    # the zip code counts for our housing dataset is *extremely*
    # unbalanced (some only having 1 or 2 houses per zip code)
    # so let's sanitize our data by removing any houses with less
    # than 25 houses per zip code
    if count < 25:
        idxs = df[df["zipcode"] == zipcode].index
        df.drop(idxs, inplace=True)
# initialize our images array (i.e., the house images themselves)
images = []

# loop over the indexes of the houses
for i in df.index.values:
    # find the four images for the house and sort the file paths,
    # ensuring the four are always in the *same order*
    basePath = os.path.sep.join([datasetPath, "{}_*".format(i + 1)])
    housePaths = sorted(list(glob.glob(basePath)))
    # initialize our list of input images along with the output image
    # after *combining* the four input images
    inputImages = []
    outputImage = np.zeros((64, 64, 3), dtype="uint8")

    # loop over the input house paths
    for housePath in housePaths:
        # load the input image, resize it to be 32 32, and then
        # update the list of input images
        image = cv2.imread(housePath)
        image = cv2.resize(image, (32, 32))
        inputImages.append(image)

    # tile the four input images in the output image such the first
    # image goes in the top-right corner, the second image in the
    # top-left corner, the third image in the bottom-right corner,
    # and the final image in the bottom-left corner
    outputImage[0:32, 0:32] = inputImages[0]
    outputImage[0:32, 32:64] = inputImages[1]
    outputImage[32:64, 32:64] = inputImages[2]
    outputImage[32:64, 0:32] = inputImages[3]

    # add the tiled image to our set of images the network will be
    # trained on
    images.append(outputImage)
images = np.array(images)
images.shape
plt.imshow(images[45][...,::-1])
images = images / 255.0
# partition the data into training and testing splits using 75% of
# the data for training and the remaining 25% for testing
split = train_test_split(df, images, test_size=0.25, random_state=42)
(trainAttrX, testAttrX, trainImagesX, testImagesX) = split
# find the largest house price in the training set and use it to
# scale our house prices to the range [0, 1] (will lead to better
# training and convergence)
maxPrice = trainAttrX["price"].max()
trainY = trainAttrX["price"] / maxPrice
testY = testAttrX["price"] / maxPrice
from tensorflow.keras.models import Model
from tensorflow.keras import Input
from tensorflow.keras.layers import Conv2D, MaxPooling2D
from tensorflow.keras.layers import Activation, Dense, Flatten, Dropout
from tensorflow.keras.layers import BatchNormalization
width, height, depth = 64, 64, 3
filters=(16, 32, 64)
# initialize the input shape and channel dimension, assuming
# TensorFlow/channels-last ordering
inputShape = (height, width, depth)
chanDim = -1

# define the model input
inputs = Input(shape=inputShape)

# loop over the number of filters
for (i, f) in enumerate(filters):
    # if this is the first CONV layer then set the input
    # appropriately
    if i == 0:
        x = inputs

    # CONV => RELU => BN => POOL
    x = Conv2D(f, (3, 3), padding="same")(x)
    x = Activation("relu")(x)
    x = BatchNormalization(axis=chanDim)(x)
    x = MaxPooling2D(pool_size=(2, 2))(x)
    
# flatten the volume, then FC => RELU => BN => DROPOUT
x = Flatten()(x)
x = Dense(16)(x)
x = Activation("relu")(x)
x = BatchNormalization(axis=chanDim)(x)
x = Dropout(0.5)(x)

# apply another FC layer, this one to match the number of nodes
# coming out of the MLP
x = Dense(4)(x)
x = Activation("relu")(x)


x = Dense(1, activation="linear")(x)

# construct the CNN
model = Model(inputs, x)
model.summary()
opt = Adam(lr=1e-3, decay=1e-3 / 200)
model.compile(loss="mean_absolute_percentage_error", optimizer=opt)
model.fit(trainImagesX, trainY, validation_data=(testImagesX, testY), 
          epochs=200, batch_size=8)<jupyter_output>Epoch 1/200
34/34 [==============================] - 4s 47ms/step - loss: 2041.0808 - val_loss: 73.9914
Epoch 2/200
34/34 [==============================] - 0s 12ms/step - loss: 1002.9117 - val_loss: 650.1738
Epoch 3/200
34/34 [==============================] - 0s 11ms/step - loss: 772.6211 - val_loss: 696.2377
Epoch 4/200
34/34 [==============================] - 0s 11ms/step - loss: 754.9536 - val_loss: 200.4412
Epoch 5/200
34/34 [==============================] - 0s 11ms/step - loss: 658.7330 - val_loss: 757.4846
Epoch 6/200
34/34 [==============================] - 0s 12ms/step - loss: 447.6073 - val_loss: 1515.0216
Epoch 7/200
34/34 [==============================] - 0s 12ms/step - loss: 429.2026 - val_loss: 2535.4543
Epoch 8/200
34/34 [==============================] - 0s 11ms/step - loss: 357.0591 - val_loss: 2401.9583
Epoch 9/200
34/34 [==============================] - 0s 11ms/step - loss: 314.2249 - val_loss: 3598.0027
Epoch 10/200
34/34 [==============================] - 0s 11[...]
{'fraction_non_alphanumeric': 0.15470417070805043, 'fraction_numerical': 0.05868089233753637, 'mean_word_length': 4.130182421227198, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 10, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '435296', 'license': 'MIT', 'n_tokens_mistral': 2246, 'n_tokens_neox': 1954, 'n_words': 660, 'path': 'cnn-and-price-regression.ipynb', 'repo_name': 'mohammedbehjoo/Deep_Architecture_using-AI_in_architecture', 'size': '6186'}