質問編集履歴

2

コードを追記しました

2021/06/12 12:13

投稿

tkrd
tkrd

スコア5

test CHANGED
File without changes
test CHANGED
@@ -27,3 +27,253 @@
27
27
  anaconda等の導入のための参考サイト↓
28
28
 
29
29
  https://qiita.com/komiya_____/items/96c14485eb035701e218
30
+
31
+
32
+
33
+
34
+
35
+
36
+
37
+ 追記(コード群)
38
+
39
+
40
+
41
+ dockerfile, docker-compose.yml, mnist.pyのコードを追記しました。mnist.pyはKeras Blogのものをほぼそのまま使ったものになります。また、有用な情報となるかわかりませんが、Dockerの環境構築は以下の記事を参考にしました。(ubuntuのバージョンは記事に合わせて18.04にしてあります)
42
+
43
+
44
+
45
+ 参考記事:https://qiita.com/karaage0703/items/e79a8ad2f57abc6872aa
46
+
47
+ Keras Blog:https://blog.keras.io/building-autoencoders-in-keras.html
48
+
49
+
50
+
51
+ ```Dockerfile
52
+
53
+ # ベースイメージ名:タグ名
54
+
55
+ FROM continuumio/anaconda3:2019.03
56
+
57
+
58
+
59
+ # pipをアップグレードし必要なパッケージをインストール
60
+
61
+ RUN pip install --upgrade pip && \
62
+
63
+ pip install autopep8 && \
64
+
65
+ pip install Keras && \
66
+
67
+ pip install tensorflow
68
+
69
+
70
+
71
+ # コンテナ側のルート直下にworkdir/(任意)という名前の作業ディレクトリを作り移動する
72
+
73
+ WORKDIR /workdir
74
+
75
+
76
+
77
+ # コンテナ側のリッスンポート番号
78
+
79
+ # 明示しているだけで、なくても動く
80
+
81
+ EXPOSE 8888
82
+
83
+
84
+
85
+ # ENTRYPOINT命令はコンテナ起動時に実行するコマンドを指定(基本docker runの時に上書きしないもの)
86
+
87
+ # "jupyter-lab" => jupyter-lab立ち上げコマンド
88
+
89
+ # "--ip=0.0.0.0" => ip制限なし
90
+
91
+ # "--port=8888" => EXPOSE命令で書いたポート番号と合わせる
92
+
93
+ # ”--no-browser” => ブラウザを立ち上げない。コンテナ側にはブラウザがないので 。
94
+
95
+ # "--allow-root" => rootユーザーの許可。セキュリティ的には良くないので、自分で使うときだけ。
96
+
97
+ # "--NotebookApp.token=''" => トークンなしで起動許可。これもセキュリティ的には良くない。
98
+
99
+ ENTRYPOINT ["jupyter-lab", "--ip=0.0.0.0", "--port=8888", "--no-browser", "--allow-root", "--NotebookApp.token=''"]
100
+
101
+
102
+
103
+ # CMD命令はコンテナ起動時に実行するコマンドを指定(docker runの時に上書きする可能性のあるもの)
104
+
105
+ # "--notebook-dir=/workdir" => Jupyter Labのルートとなるディレクトリを指定
106
+
107
+ CMD ["--notebook-dir=/workdir"]
108
+
109
+
110
+
111
+
112
+
113
+ ```
114
+
115
+
116
+
117
+ ```yaml
118
+
119
+ version: '3' # docker-composeファイルの書式バージョン。最新の’3’を指定(2021/6/12現在)
120
+
121
+ services:
122
+
123
+ dev: # 任意の名前(ディレクトリ名 + dev がコンテナ名となります)
124
+
125
+ build:
126
+
127
+ context: .
128
+
129
+ dockerfile: Dockerfile_study
130
+
131
+ image: study
132
+
133
+ ports:
134
+
135
+ - "8080:8888"
136
+
137
+ volumes:
138
+
139
+ - .:/workdir
140
+
141
+
142
+
143
+ ```
144
+
145
+
146
+
147
+ ```Python
148
+
149
+ from keras.layers import Input, Dense
150
+
151
+ from keras.models import Model
152
+
153
+ from keras.datasets import mnist
154
+
155
+ import numpy as np
156
+
157
+ from sklearn.model_selection import train_test_split
158
+
159
+ import matplotlib.pyplot as plt
160
+
161
+
162
+
163
+ # encoderの次元
164
+
165
+ encoding_dim = 32
166
+
167
+
168
+
169
+ # 入力用の変数
170
+
171
+ input_img = Input(shape=(784, ))
172
+
173
+ # 入力された画像がencodeされたものを格納する変数
174
+
175
+ encoded = Dense(encoding_dim, activation='relu')(input_img)
176
+
177
+ # ecnodeされたデータを再構成した画像を格納する変数
178
+
179
+ decoded = Dense(784, activation='sigmoid')(encoded)
180
+
181
+ # 入力画像を再構成するModelとして定義
182
+
183
+ autoencoder = Model(input_img, decoded)
184
+
185
+
186
+
187
+ # 入力する画像をencodeする部分
188
+
189
+ encoder = Model(input_img, encoded)
190
+
191
+ encoded_input = Input(shape=(encoding_dim, ))
192
+
193
+ decoder_layer = autoencoder.layers[-1]
194
+
195
+ # encodeされた画像データを再構成する部分
196
+
197
+ decoder = Model(encoded_input, decoder_layer(encoded_input))
198
+
199
+
200
+
201
+ # AdaDeltaで最適化, loss関数はbinary_crossentropy
202
+
203
+ autoencoder.compile(optimizer='adam', loss='binary_crossentropy')
204
+
205
+
206
+
207
+ # MNISTデータを前処理する
208
+
209
+ (x_train, _), (x_test, _) = mnist.load_data()
210
+
211
+ x_train, x_valid = train_test_split(x_train, test_size=0.175)
212
+
213
+ x_train = x_train.astype('float32')/255.
214
+
215
+ x_valid = x_valid.astype('float32')/255.
216
+
217
+ x_test = x_test.astype('float32')/255.
218
+
219
+ x_train = x_train.reshape((len(x_train), np.prod(x_train.shape[1:])))
220
+
221
+ x_valid = x_valid.reshape((len(x_valid), np.prod(x_valid.shape[1:])))
222
+
223
+ x_test = x_test.reshape((len(x_test), np.prod(x_test.shape[1:])))
224
+
225
+
226
+
227
+ # autoencoderの実行
228
+
229
+ autoencoder.fit(x_train, x_train,
230
+
231
+ epochs=50,
232
+
233
+ batch_size=256,
234
+
235
+ shuffle=True,
236
+
237
+ validation_data=(x_valid, x_valid))
238
+
239
+
240
+
241
+ # 画像化して確認
242
+
243
+ encoded_img = encoder.predict(x_test)
244
+
245
+ decoded_img = decoder.predict(encoded_img)
246
+
247
+
248
+
249
+ n = 10
250
+
251
+ plt.figure(figsize=(20, 4))
252
+
253
+ for i in range(n):
254
+
255
+ ax = plt.subplot(2, n, i+1)
256
+
257
+ plt.imshow(x_test[i].reshape(28, 28))
258
+
259
+ plt.gray()
260
+
261
+ ax.get_xaxis().set_visible(False)
262
+
263
+ ax.get_yaxis().set_visible(False)
264
+
265
+
266
+
267
+ ax = plt.subplot(2, n, i+1+n)
268
+
269
+ plt.imshow(decoded_img[i].reshape(28, 28))
270
+
271
+ plt.gray()
272
+
273
+ ax.get_xaxis().set_visible(False)
274
+
275
+ ax.get_yaxis().set_visible(False)
276
+
277
+ plt.show()
278
+
279
+ ```

1

キャプチャを追加しました。

2021/06/12 12:13

投稿

tkrd
tkrd

スコア5

test CHANGED
File without changes
test CHANGED
@@ -14,6 +14,12 @@
14
14
 
15
15
 
16
16
 
17
+ VScodeのポートのキャプチャです。
18
+
19
+ ![イメージ説明](7e5362bdd7dcc2b7b55dc70e24ee0c67.png)
20
+
21
+
22
+
17
23
  よろしくお願いいたします。
18
24
 
19
25