질문딥러닝 코드 도와주세요!!
안녕하세요, 다름이 아니라 코드를 진행 하는 과정 중 궁금한 점 몇가지가 있어 질문드립니다. 아래와 같이 코드를 돌렸을 때 iou 의 값이 0.37778513125965146에서 비슷하거나 오르지가 않는데 혹시 어떻게 하면 올릴 수 있나요? cell의 개수를 세고 싶어 코드를 마지막에 작성했는데, ValueError: buffer is not large enough 오류가 뜹니다. 이 오류의 해결 방법을 알 수 있을까요? 오류 내용: File "C:Users.spyder-py3practice1_051423.py", line 204, in count_cells(prediction_other[test_img_number], t = 65) File "C:Users.spyder-py3practice1_051423.py", line 173, in count_cells plt.imsave('test.png', img_mask) File "C:Usersanaconda3libsite-packagesmatplotlibpyplot.py", line 2165, in imsave return matplotlib.image.imsave(fname, arr, **kwargs) File "C:Usersanaconda3libsite-packagesmatplotlibimage.py", line 1645, in imsave image = PIL.Image.frombuffer( File "C:Usersanaconda3libsite-packagesPILImage.py", line 2893, in frombuffer im = im._new(core.map_buffer(data, size, decoder_name, 0, args)) ValueError: buffer is not large enough 코드: from simple_unet_model import simple_unet_model # Use normal unet model from keras.utils import normalize import os import cv2 from PIL import Image import numpy as np from matplotlib import pyplot as plt image_directory = r"C:UsersDesktopVS Codecell_images" mask_directory = r"C:UsersDesktopVS Codecell_masks" SIZE = 256 image_dataset = [] mask_dataset = [] images = os.listdir(image_directory) for i, image_name in enumerate(images): if (image_name.split('.')[1] == 'png'): image_path = os.path.join(image_directory, image_name) image = cv2.imread(image_path,0) if image is None: print(f"Unable to read image file: {image_path}") continue image = Image.fromarray(image) image = image.resize((SIZE, SIZE)) image_dataset.append(np.array(image)) masks = os.listdir(mask_directory) for i, image_name in enumerate(masks): if (image_name.split('.')[1] == 'png'): image_path = os.path.join(mask_directory, image_name) image = cv2.imread(image_path,0) if image is None: print(f"Unable to read image file: {image_path}") continue image = Image.fromarray(image) image = image.resize((SIZE, SIZE)) mask_dataset.append(np.array(image)) # Normalize images image_dataset = np.expand_dims(normalize(np.array(image_dataset), axis = 1),3) #D not normalize masks, just rescale to 0 to 1. mask_dataset = np.expand_dims((np.array(mask_dataset)),3) /255 from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(image_dataset, mask_dataset, test_size = 0.10, random_state = 0) # Sanity check, view few images import random import numpy as np image_number = random.randint(0, len(X_train)) plt.figure(figsize = (12, 6)) plt.subplot(121) plt.imshow(np.reshape(X_train[image_number], (256, 256)), cmap = 'gray') plt.subplot(122) plt.imshow(np.reshape(y_train[image_number], (256, 256)), cmap = 'gray') plt.show() ####################################################################### IMG_HEIGHT = image_dataset.shape[1] IMG_WIDTH = image_dataset.shape[2] IMG_CHANNELS = image_dataset.shape[3] def get_model(): return simple_unet_model(IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS) model = get_model() # if starting with pre-trained weights. # model.Load_weights('mitochondria_gpu_tf1.4.hdf5) history = model.fit(X_train, y_train, batch_size = 16, verbose = 1, epochs = 50, validation_data = (X_test, y_test), shuffle = False) model.save('cell_test1.hdf5') ########################################################### # Evaluate the model _, accuracy = model.evaluate(X_test, y_test) print("Accuracy = ", (accuracy * 100), "%") # Plot the training and validation accuracy and Loss at each epoch loss = history.history['loss'] val_loss = history.history['val_loss'] epochs = range(1, len(loss) + 1) plt.plot(epochs, loss, 'y', label = 'Training loss') plt.plot(epochs, val_loss, 'r', label = 'Validation loss') plt.title('Training and validation loss') plt.xlabel('Epochs') plt.ylabel('Loss') plt.legend() plt.show() accuracy = history.history['accuracy'] # acc = accuracy val_accuracy = history.history['val_accuracy'] plt.plot(epochs, accuracy, 'y', label = 'Training acc') plt.plot(epochs, val_accuracy, 'r', label = 'Validation acc') plt.title('Training and validation accuracy') plt.xlabel('Epochs') plt.ylabel('Accuracy') plt.legend() plt.show() **################################## #IOU y_pred=model.predict(X_test) y_pred_thresholded = y_pred > 0.5 intersection = np.logical_and(y_test, y_pred_thresholded) union = np.logical_or(y_test, y_pred_thresholded) intersection = np.sum(intersection) union = np.sum(union) iou_score = intersection / union print("IoU score is: ", iou_score)** ############################################################## # predict on a few images model.load_weights('cell_test1.hdf5') test_img_number = random.randint(0, len(X_test)) test_img = X_test[test_img_number] ground_truth = y_test[test_img_number] test_img_norm = test_img[:,:,0][:,:,None] test_img_input = np.expand_dims(test_img_norm, 0) prediction = (model.predict(test_img_input)[0,:,:,0] > 0.5).astype(np.uint8) test_img_other = cv2.imread(r"C:UsersDesktopVS Codecell_imagesMar24bS1C2R1_DMl_200x_y.png", 0) test_img_other_norm = np.expand_dims(normalize(np.array(test_img_other), axis = 1), 2) test_img_other_norm = test_img_other_norm[:,:,0][:,:,None] test_img_other_input = np.expand_dims(test_img_other_norm, 0) prediction_other = (model.predict(test_img_other_input)[0,:,:,0] > 0.5).astype(np.uint8) plt.figure(figsize = (16, 8)) plt.subplot(231) plt.title("Testing Image") plt.imshow(test_img[:,:,0], cmap = 'gray') plt.subplot(232) plt.title('Testing Lable') plt.imshow(ground_truth[:,:,], cmap = 'gray') plt.subplot(233) plt.title("Prediction on test image") plt.imshow(prediction, cmap = 'gray') plt.subplot(234) plt.title("External Image") plt.imshow(test_img_other, cmap = 'gray') plt.subplot(235) plt.title('Prediction of external Image') plt.imshow(prediction_other, cmap = 'gray') plt.show() ########################################################################## **# count cells using predicted mask original_img = test_img[test_img_number] img0 = original_img.copy() #img0 = np.asarray(original_img) plt.imshow(img0) plt.show() def count_cells(pred_mask, t = 65): img_mask = np.squeeze(pred_mask) plt.imsave('test.png', img_mask) image = Image.open('test.png').convert("L") img = np.asarray(image) img = img.copy() blur = cv2.GaussianBlur(img, (5, 5), 0) (t, binary) = cv2.threshold(blur, t, 255, cv2.THRESH_BINARY) # find contours (_, contours, _) = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CAHIN_APPROS_SIMPLE) # create all-black mask image mask = np.zeros(img.shape, dtype = 'uint8') # draw white rectangles for each object's bounding box for c in contours: (x, y, w, h) = cv2.boundingRect(c) cv2.rectangle(mask, (x, y), (x + w, y + h), (255, 255, 255), -1) # draw contours over original image cv2.drawContours(img, contours, -1, (255, 100, 100), 1) plt.imshow(img) plt.show() # print table of contours and sizes print("Found {} cells".format(len(contours))) for (i, c) in enumerate(contours): print(" Size of cell {}: {}".format(i, len(c))) return count_cells(prediction_other[test_img_number], t = 65)**