rawファイルから画像を読み込み,画像分類をCNNで実装したいです.
画像群は脊椎のもので,健常か否かを識別したいです.
RuntimeErrorが発生しています.
発生している問題・エラーメッセージ
<_io.BufferedReader name='./case2.raw'>
RuntimeError Traceback (most recent call last)
<ipython-input-14-cf12a435979a> in <module>
73
74 #学習の実行
---> 75 history = model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=verbose, validation_data=(x_test, y_test))
76
77 #モデルの評価
~\Anaconda3\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
950 sample_weight=sample_weight,
951 class_weight=class_weight,
--> 952 batch_size=batch_size)
953 # Prepare validation data.
954 do_validation = False
~\Anaconda3\lib\site-packages\keras\engine\training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
679 if y is not None:
680 if not self.optimizer:
--> 681 raise RuntimeError('You must compile a model before '
682 'training/testing. '
683 'Use model.compile(optimizer, loss)
.')
RuntimeError: You must compile a model before training/testing. Use model.compile(optimizer, loss)
.
実行したソースコード
#ライブラリのインポート
import keras
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Dense, Dropout, Flatten
from keras.optimizers import Adam
import time
coding:utf-8
encoding = "utf-8"
import keras
from keras.utils import np_utils
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import numpy as np
必要なモジュールをインポート
from keras.applications.vgg16 import VGG16, decode_predictions,preprocess_input
from keras.preprocessing import image
from PIL import Image
import numpy as np
import urllib.request as urllib
手元の環境で実行させたい場合は下記2つも読み込んでください。
import tensorflow
import keras
import SimpleITK as sitk
"""
filename: 判定したい画像ファイル
size: 予測した結果を何件まで表示させたいか(初期値10件)
"""
def predict(filename, size=5):
filename = "./case1.raw" # 入力画像をWebから取得 img = image.load_img(filename, target_size=(512, 512)) # 画像を読み込み x = image.img_to_array(img) # 画像ファイルを数値に変換 y = np.expand_dims(x, axis=0) # 次元を増やす pred = model.predict(preprocess_input(x)) # 一律に平均値を引いている処理 results = decode_predictions(pred, top=size)[0] # VGG16の1000クラスはdecode_predictions()で文字列に変換 return results
fd = open("./case2.raw", 'rb')
height = 512
width = 512
f = np.fromfile(fd, dtype=np.float64, count=height*width)
img = f.reshape((height,width))
print(fd)
(x_train, y_train), (x_test, y_test) = ('rb'), ('rb')
x_train = f.reshape(65536,-1)
x_test = f.reshape(65536, -1)
x_train = x_train.reshape(x_train.shape[0], -1)
x_test = x_test.reshape(x_test.shape[0], -1)
x_train = x_train.astype('float32')/256
x_test = x_test.astype('float32')/256
epochs = 10
batch_size = 512
verbose = 1
model = Sequential()
#学習の実行
history = model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=verbose, validation_data=(x_test, y_test))
#モデルの評価
score = model.evaluate(x_test, y_test)
試したこと
UTF-8にする記述を付け加えました.