欠損画像を作りたいのですがエラーが生じてしまいます…
#該当コード
from keras import layers
from keras import models
import matplotlib.pyplot as plt
from tensorflow.keras.datasets import mnist
import numpy as np
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
train_images = train_images.reshape((60000, 28, 28, 1))
train_images = train_images.astype('float32') / 255
test_images = test_images.reshape((10000, 28, 28, 1))
test_images = test_images.astype('float32') / 255
test_images_noisy = test_images[:, 13:15, 13:15, 0]=0
test_images_noisy =test_images_noisy.astype('float32') / 255
#エラー
File "C:\Users\user.spyder-py3\untitled6.py", line 27, in <module>
test_images_noisy =test_images_noisy.astype('float32') / 255
AttributeError: 'int' object has no attribute 'astype'
最後の行でエラーが出ているのですが解決方法がわかりません。
どなたかわかるかたいらっしゃったらお願いします。
#追記
(題名変えました)
前半に続くコードです。
def create_model():
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), padding='same', activation='relu', input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D((2, 2), padding='same'))
model.add(layers.Conv2D(16, (3, 3), padding='same', activation='relu'))
model.add(layers.MaxPooling2D((2, 2), padding='same'))
model.add(layers.Conv2D(8, (3, 3), padding='same', activation='relu'))
model.add(layers.MaxPooling2D((2, 2), padding='same'))
model.add(layers.Conv2D(8, (3, 3), padding='same', activation='relu')) model.add(layers.UpSampling2D((2, 2))) model.add(layers.Conv2D(16, (3, 3), padding='same', activation='relu')) model.add(layers.UpSampling2D((2, 2))) model.add(layers.Conv2D(32, (3, 3), activation='relu')) model.add(layers.UpSampling2D((2, 2))) model.add(layers.Conv2D(1, (3, 3), padding='same', activation='sigmoid')) model.compile(loss='binary_crossentropy', optimizer='sgd') return model
model = create_model()
def show_result(j):
results = model.predict(test_images_noisy_2[j:j+10])
fig = plt.figure(figsize=(16, 2.7)) for i in range(10): subplot = fig.add_subplot(3, 10, i+1) subplot.set_xticks([]) subplot.set_yticks([]) subplot.imshow(test_images_noisy_2[j+i, :, :, 0], vmin=0, vmax=1, cmap=plt.cm.gray_r) subplot = fig.add_subplot(3, 10, i+11) subplot.set_xticks([]) subplot.set_yticks([]) subplot.imshow(results[i, :, :, 0], vmin=0, vmax=1, cmap=plt.cm.gray_r) subplot = fig.add_subplot(3, 10, i+21) subplot.set_xticks([]) subplot.set_yticks([]) subplot.imshow(np.abs(test_images_noisy_2[j+i, :, :, 0]-results[i, :, :, 0]), vmin=0, vmax=1, cmap=plt.cm.gray_r)
train_num = 10
learn = model.fit(train_images[:30000], train_images[:30000], batch_size=16, epochs=1)
model.save('cnn_auto_model_noisy.h5')
#実行結果
ここでshow_result(0)で実行すると
runfile('C:/Users/user/.spyder-py3/untitled6.py', wdir='C:/Users/user/.spyder-py3')
Epoch 1/1
30000/30000 [==============================] - 71s 2ms/step - loss: 0.2707
show_result(0)
Traceback (most recent call last):
File "<ipython-input-100-84299c05f901>", line 1, in <module>
show_result(0)
File "C:\Users\user.spyder-py3\untitled6.py", line 60, in show_result
IndexError: invalid index to scalar variable.
がでてしまいます。