text_plain_output_1.png
from sklearn.model_selection import train_test_split

from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, Callback, ReduceLROnPlateau

from tensorflow.keras.layers import BatchNormalization,Activation,Dropout,Dense

from tensorflow.keras.layers import Flatten, Conv2D, MaxPooling2D

from tensorflow.keras.models import Sequential

import cv2

import numpy as np

import numpy as np # linear algebra

import os

import os

import pandas as pd

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import random

import re

import tensorflow as tf

import numpy as np
import pandas as pd
import os

def seed_everything(seed):
    random.seed(seed)
    os.environ['PYTHONHASHSEED'] = str(seed)
    np.random.seed(seed)
    tf.random.set_seed(seed)

def load_images(path):
    images = []
    bedroom = []
    bathroom = []
    frontal = []
    kitchen = []
    pattern = re.compile('([0-9]{1,3})_(bathroom|bedroom|frontal|kitchen).jpg$')
    files = os.listdir(path=path)
    files.sort()
    for filename in files:
        if pattern.match(filename) is None:
            continue
        img = cv2.imread(path + filename)
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        img = cv2.resize(img, (IMAGE_SIZE, IMAGE_SIZE))
        if 'bedroom' in filename:
            bedroom.append(img)
        if 'bathroom' in filename:
            bathroom.append(img)
        if 'front' in filename:
            frontal.append(img)
        if 'kitchen' in filename:
            kitchen.append(img)
    for i in range(len(bedroom)):
        tiles = [[bedroom[i], bathroom[i]], [frontal[i], kitchen[i]]]
        image_concat = cv2.vconcat([cv2.hconcat(v_list) for v_list in tiles])
        images.append(image_concat)
    return np.array(images) / 255.0

def create_cnn():
    model = Sequential()
    inputShape = (IMAGE_SIZE * 2, IMAGE_SIZE * 2, 3)
    '\n    演習:kernel_sizeを変更してみてください\n    '
    kernel_size = (5, 5)
    model.add(Conv2D(filters=32, kernel_size=kernel_size, strides=(1, 1), padding='valid', activation='relu', kernel_initializer='he_normal', input_shape=inputShape))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(BatchNormalization())
    model.add(Dropout(0.1))
    model.add(Conv2D(filters=64, kernel_size=kernel_size, strides=(1, 1), padding='valid', activation='relu', kernel_initializer='he_normal'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(BatchNormalization())
    model.add(Dropout(0.1))
    model.add(Conv2D(filters=128, kernel_size=(2, 2), strides=(1, 1), padding='valid', activation='relu', kernel_initializer='he_normal'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(BatchNormalization())
    model.add(Dropout(0.1))
    '\n    演習:もう一層Conv2D->MaxPooling2D->BatchNormalization->Dropoutを追加してください\n    '
    model.add(Flatten())
    model.add(Dense(units=256, activation='relu', kernel_initializer='he_normal'))
    model.add(Dense(units=32, activation='relu', kernel_initializer='he_normal'))
    model.add(Dense(units=1, activation='linear'))
    model.compile(loss='mape', optimizer='adam', metrics=['mape'])
    return model

def kfold(train_images_x, train_y, valid_images_x, valid_y):
    k = 1
    train_x = train_images_x
    valid_x = valid_images_x
    num_val_samples = len(train_x) // k
    num_epochs = 32
    all_scores = []
    all_mape_histories = []
    filepath = 'cnn_best_model.hdf5'
    es = EarlyStopping(patience=5, mode='min', verbose=1)
    checkpoint = ModelCheckpoint(monitor='val_loss', filepath=filepath, save_best_only=True, mode='auto')
    reduce_lr_loss = ReduceLROnPlateau(monitor='val_loss', patience=2, verbose=1, mode='min')
    for i in range(k):
        val_data = train_x[i * num_val_samples:(i + 1) * num_val_samples]
        val_targets = train_y[i * num_val_samples:(i + 1) * num_val_samples]
        partial_train_data = np.concatenate([train_x[:i * num_val_samples], train_x[(i + 1) * num_val_samples:]], axis=0)
        partial_train_targets = np.concatenate([train_y[:i * num_val_samples], train_y[(i + 1) * num_val_samples:]], axis=0)
        model = create_cnn()
        history = model.fit(partial_train_data, partial_train_targets, validation_data=(valid_x, valid_y), epochs=num_epochs, batch_size=1, verbose=0, callbacks=[es, checkpoint, reduce_lr_loss])
        val_mse, val_mape = model.evaluate(valid_x, valid_y, verbose=0)
        all_scores.append(val_mape)
        mape_history = history.history['val_mape']
        all_mape_histories.append(mape_history)
        return (all_scores, all_mape_histories)

def leave_one_out(train_image_x, train_y, valid_images_x, valid_y):
    es = EarlyStopping(patience=5, mode='min', verbose=1)
    checkpoint = ModelCheckpoint(monitor='val_loss', filepath=PATH_TO_HDF5, save_best_only=True, mode='auto')
    reduce_lr_loss = ReduceLROnPlateau(monitor='val_loss', patience=2, verbose=1, mode='min')
    inputShape = (IMAGE_SIZE * 2, IMAGE_SIZE * 2, 3)
    model = create_cnn()
    model.fit(train_images_x, train_y, validation_data=(valid_images_x, valid_y), epochs=50, batch_size=16, callbacks=[es, checkpoint, reduce_lr_loss])
    return model

train = pd.read_csv(PATH_TO_TRAIN)

train_images = load_images(PATH_TO_TRAIN_IMAGE)
test_images = load_images(PATH_TO_TEST_IMAGE)

train_x, valid_x, train_images_x, valid_images_x = train_test_split(train, train_images, test_size=PER_TEST)
train_y = train_x['price'].values
valid_y = valid_x['price'].values
mean = train_x.mean(axis=0)
train_x -= mean
std = train_x.std(axis=0)
train_x /= std
valid_x -= mean
valid_x /= std

model = leave_one_out(train_images_x, train_y, valid_images_x, valid_y)

y_pred = model.predict(test_images, batch_size=32)

def mean_absolute_percentage_error(y_true, y_pred):
    y_true, y_pred = (np.array(y_true), np.array(y_pred))
    return np.mean(np.abs((y_true - y_pred) / y_true)) * 100
model.load_weights(PATH_TO_HDF5)
valid_pred = model.predict(valid_images_x, batch_size=32).reshape((-1, 1))
mape_score = mean_absolute_percentage_error(valid_y, valid_pred)
print(mape_score)