質問編集履歴
1
該当ソースコードのところに全体のコードを追加しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,5 +1,12 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
2
|
convLSTMを用いた画像予測を行っています。
|
3
|
+
|
4
|
+
%matplotlib inline
|
5
|
+
i=0
|
6
|
+
fig, axes = plt.subplots(1, 2, figsize=(12,6))
|
7
|
+
axes[0].imshow((y_test[i]+1)/2)
|
8
|
+
axes[1].imshow((model.predict(x_test[[i]]).reshape(100,180,3)+1)/2)
|
9
|
+
|
3
10
|
i=0のところを0以外の数値にするとエラーが出ます。
|
4
11
|
|
5
12
|
|
@@ -12,12 +19,81 @@
|
|
12
19
|
### 該当のソースコード
|
13
20
|
|
14
21
|
```ここに言語名を入力
|
22
|
+
|
23
|
+
import numpy as np
|
24
|
+
import matplotlib.pyplot as plt
|
25
|
+
import pandas as pd
|
26
|
+
import seaborn as sns
|
27
|
+
from sklearn.model_selection import train_test_split
|
28
|
+
import glob
|
29
|
+
from PIL import Image
|
30
|
+
from tqdm import tqdm
|
31
|
+
import zipfile
|
32
|
+
import io
|
33
|
+
|
34
|
+
|
35
|
+
|
36
|
+
# 縮小後の画像サイズ
|
37
|
+
height = 100
|
38
|
+
width = 180
|
39
|
+
|
40
|
+
# 読み込んだ画像を入れる配列
|
41
|
+
imgs=np.empty((0, height, width, 3))
|
42
|
+
|
43
|
+
# zipファイルを読み込んでnumpy配列にする
|
44
|
+
zip_f = zipfile.ZipFile('drive/My Drive/Colab Notebooks/convLSTM/wide.zip')
|
45
|
+
for name in tqdm(zip_f.namelist()):
|
46
|
+
with zip_f.open(name) as file:
|
47
|
+
path = io.BytesIO(file.read()) #解凍
|
48
|
+
img = Image.open(path)
|
49
|
+
img = img.resize((width, height))
|
50
|
+
img_np = np.array(img).reshape(1, height, width, 3)
|
51
|
+
imgs = np.append(imgs, img_np, axis=0)
|
52
|
+
|
53
|
+
|
54
|
+
# 時系列で学習できる形式に整える
|
55
|
+
n_seq = 5
|
56
|
+
n_sample = imgs.shape[0] - n_seq
|
57
|
+
|
58
|
+
x = np.zeros((n_sample, n_seq, height, width, 3))
|
59
|
+
y = np.zeros((n_sample, height, width, 3))
|
60
|
+
for i in range(n_sample):
|
61
|
+
x[i] = imgs[i:i+n_seq]
|
62
|
+
y[i] = imgs[i+n_seq]
|
63
|
+
x, y = (x-128)/128, (y-128)/128
|
64
|
+
|
65
|
+
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.1, shuffle = False)
|
66
|
+
|
67
|
+
|
68
|
+
from keras import layers
|
69
|
+
from keras.layers.core import Activation
|
70
|
+
from tensorflow.keras.models import Model
|
71
|
+
|
72
|
+
inputs = layers.Input(shape=(5, height, width, 3))
|
73
|
+
x0 = layers.ConvLSTM2D(filters=16, kernel_size=(3,3), padding="same", return_sequences=True, data_format="channels_last")(inputs)
|
74
|
+
x0 = layers.BatchNormalization(momentum=0.6)(x0)
|
75
|
+
x0 = layers.ConvLSTM2D(filters=16, kernel_size=(3,3), padding="same", return_sequences=True, data_format="channels_last")(x0)
|
76
|
+
x0 = layers.BatchNormalization(momentum=0.8)(x0)
|
77
|
+
|
78
|
+
x0 = layers.ConvLSTM2D(filters=3, kernel_size=(3,3), padding="same", return_sequences=False, data_format="channels_last")(x0)
|
79
|
+
out = Activation('tanh')(x0)
|
80
|
+
model = Model(inputs=inputs, outputs=out)
|
81
|
+
model.summary()
|
82
|
+
|
83
|
+
|
84
|
+
model.compile(optimizer='rmsprop',
|
85
|
+
loss='mae', metrics=['mse'])
|
86
|
+
call_backs=[EarlyStopping(monitor="val_loss",patience=5)]
|
87
|
+
model.fit(x_train, y_train, batch_size=16, epochs=100, verbose=2, validation_split=0.2, shuffle=True, callbacks=call_backs)
|
88
|
+
|
15
89
|
# 描画
|
16
90
|
%matplotlib inline
|
17
|
-
i=
|
91
|
+
i=10
|
18
92
|
fig, axes = plt.subplots(1, 2, figsize=(12,6))
|
19
93
|
axes[0].imshow((y_test[i]+1)/2)
|
20
94
|
axes[1].imshow((model.predict(x_test[[i]]).reshape(100,180,3)+1)/2)
|
95
|
+
|
96
|
+
|
21
97
|
```
|
22
98
|
|
23
99
|
|