#딥러닝

뎁스_2599뎁스_2599· 2년

질문이미지 인식의 정확도를 높이고 싶습니다.

안녕하세요, 저는 움직이는 영상에서 특정 물체를 인식하는 코드를 만들고자 합니다. 다만 유튜브와 깃허브에서 나온 자료들을 활용하여 만들었으나 물체를 인식하는데 정확도가 비교적 떨어집니다. 아래의 코드에서 어떻게 정확도를 높일 수 있을까요? 분석하고자 하는 영상은 아래의 링크와 같으며, 떠다니는 구모양의 powder의 갯수를 세는 것이 목적입니다. 참고 영상: https://www.youtube.com/watch?v=BtKI0_jdjE8 import cv2 from tracker import * # create tracker object tracker = EuclideanDistTracker() cap=cv2.VideoCapture(r"C:\Users\Desktop\spyder\yolov8-custom-object-training-tracking-main\yolov8-custom-object-training-tracking-main\surf\surf.mp4") # Object detectino from Stable camera object_detector = cv2.createBackgroundSubtractorMOG2(history = 100, varThreshold = 40) while True: ret, frame = cap.read() height, width, _ = frame.shape # 1. Object Detection mask = object_detector.apply(frame) _, mask = cv2.threshold(mask, 244, 255, cv2.THRESH_BINARY) contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) detections = [] for cnt in contours: # Calculate area and remove small elements area = cv2.contourArea(cnt) if area > 100: # cv2.drawContours(roi, [cnt], -1, (0, 255, 0), 2) x, y, w, h = cv2.boundingRect(cnt) cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 3) detections.append([x, y, w, h]) # 2. Object Tracking boxes_ids = tracker.update(detections) for box_id in boxes_ids: x, y, w, h, id = box_id cv2.putText(frame, str(id), (x, y - 15), cv2.FONT_HERSHEY_PLAIN, 1, (255, 0, 0), 2) cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 3) cv2.imshow("Frame", frame) cv2.imshow("Mask", mask) key = cv2.waitKey(30) if key == 27: break cap.release() cv2.destroyAllwindows()
528
1
0
피토니
피토니·2023-06-01
몇 가지 방법을 추가로 적용하여 정확도를 높일 수 있습니다. 이미지 전처리 이미지의 노이즈를 제거하거나 contrast를 향상시키는 등의 이미지 전처리 기법을 사용하여 값을 조금씩 조정하면서 물체 검출 성능을 향상시킬 수 있습니다. 예를 들면 다음과 같은 코드가 될 수 있습니다. # 이미지 전처리 # 1. 가우시안 블러로 노이즈 제거 : 가우시안 블러는 ...
뎁스_2599뎁스_2599· 2년

질문딥러닝 코드 도와주세요!!

안녕하세요, 다름이 아니라 코드를 진행 하는 과정 중 궁금한 점 몇가지가 있어 질문드립니다. 아래와 같이 코드를 돌렸을 때 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)**
513
3
0
피토니
피토니·2023-05-15
iou 값을 올리는 방법은 몇가지가 있을 수 있습니다. YOLOv4, EfficientDet, RetinaNet 등과 같은 최신 딥러닝 모델을 사용합니다.학습 데이터를 더 많이 생성하여 모델의 일반화 성능을 향상시키는 데이터 확장을 사용합니다. 예를 들어 이미지 회전, 확대/축소, 잘라내기, 뒤집기, 색상 변환 등의 방법을 사용하여 데이터를 확장할 수 있습...
뎁스_2599뎁스_2599· 2년

질문딥러닝 코드 질문

안녕하세요, 현재 딥러닝으로 세포 image segmentaion을 하려고 하는데요. 제가 코드를 돌리면 iou = 0이 뜨고, 아래의 첨부한 그림과 같이 그래프가 뜨질 않습니다. 혹시 어떤 부분을 수정해야할까요? 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:\Users\Desktop\VS Code\cell_images" mask_directory = r"C:\Users\Desktop\VS Code\cell_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 = 1, 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.0), "%") # 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 intersection = np.logical_and(y_test, y_pred_thresholded) union = np.logical_or(y_test, y_pred_thresholded) iou_score = np.sum(intersection) / np.sum(union) print("IoU socre is: ", iou_score) ############################################################## # predict on a few images model = get_model() 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.2).astype(np.uint8) test_img_other = cv2.imread(r"C:\Users\Desktop\VS Code\cell_images\37_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.2).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()
202
3
0
피토니
피토니·2023-05-14
epochs를 1로 설정했기 때문에 학습이 제대로 이루어지지 않았을 수 있습니다. 딥러닝 모델을 학습할 때 epochs를 충분히 설정하여 학습을 시켜야 모델이 세포 이미지를 segmenting하는 것을 잘 학습할 수 있습니다. 10이나 20 정도로 올려보시기 바랍니다. 그리고 IoU를 계산하는 부분에서 np.sum() 함수는 전체 배열의 요소를 합산하기 ...