qubvel / segmentation_models

Segmentation models with pretrained backbones. Keras and TensorFlow Keras.
MIT License
4.74k stars 1.03k forks source link

Applying to the Cifar 10 #114

Closed eric333316152 closed 5 years ago

eric333316152 commented 5 years ago

Thank you for your sharing. I think it is really effective API. But I got a problem When I apply to the CIFAR 10 images, I got this error. How I can fix it?

Train on 50000 samples, validate on 10000 samples Epoch 1/10 49984/50000 [============================>.] - ETA: 0s - loss: -55.6413 - iou_score: 4.4150

InvalidArgumentError Traceback (most recent call last)

in () 27 batch_size=32, 28 epochs=10, ---> 29 validation_data=(x_val, y_val), 30 ) 5 frames /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg) 526 None, None, 527 compat.as_text(c_api.TF_Message(self.status.status)), --> 528 c_api.TF_GetCode(self.status.status)) 529 # Delete the underlying status object from memory otherwise it stays alive 530 # as there is a reference to status from this from the traceback due to InvalidArgumentError: Incompatible shapes: [16,1] vs. [16,32,32,1] [[{{node loss_18/sigmoid_loss/mul_1}}]] [[{{node loss_18/mul}}]]
Diyago commented 5 years ago

You are definitely passing wrong data into your network. Could you share your code or you can manually take look your batches by yourself?

qubvel commented 5 years ago

Hi @eric333316152 As far as I know, CIFAR is classification dataset, not segmentation, so you have to use another kind of network architectures.

eric333316152 commented 5 years ago

@Diyago @qubvel I think u guys are right ....... CIFAR 10 data is inappropriate for this problem. I am just a AI beginner. So, could you please let me know where I can get data set for segmentation?

eric333316152 commented 5 years ago

@qubvel @Diyago ah before, can u please see my code? I used data set in other github, which is here : https://github.com/divamgupta/image-segmentation-keras

And here is my code.

from segmentation_models import Unet

model = Unet()

model = Unet('resnet34', encoder_weights='imagenet')

model = Unet('resnet34', classes=51, activation='softmax')

model = Unet('resnet34', input_shape=(None, None, 3), encoder_weights='imagenet') -> until here, just import your code

import cv2 import numpy as np

ann_img = np.zeros((30,30,3)).astype('uint8') ann_img[ 3 , 4 ] = 1 # this would set the label of pixel 3,4 as 1

cv2.imwrite( "ann_1.png" ,ann_img ) -> code for preparing for trained code, But I DONT know how to apply to total dataset

model = Unet('resnet34', input_shape=(416, 608, 3), encoder_weights='imagenet')

from keras.preprocessing.image import ImageDataGenerator, load_img from keras.utils import to_categorical from sklearn.model_selection import train_test_split import matplotlib.pyplot as plt import random import matplotlib.pyplot as plt import random sample = random.choice(x_train) image = load_img("/content/gdrive/My Drive/dataset1/dataset1/images_prepped_train/"+sample) plt.imshow(image) -> just mounting for google-drive of dataset, and checking image

from segmentation_models import Unet from segmentation_models.backbones import get_preprocessing from segmentation_models.losses import bce_jaccard_loss from segmentation_models.metrics import iou_score

BACKBONE = 'resnet34' preprocess_input = get_preprocessing(BACKBONE)

preprocess input

x_train = preprocess_input(x_train) x_test = preprocess_input(x_test)

define model

model = Unet(BACKBONE, encoder_weights='imagenet') model.compile('Adam', loss=bce_jaccard_loss, metrics=[iou_score])

fit model

if you use data generator use model.fit_generator(...) instead of model.fit(...)

more about fit_generator here: https://keras.io/models/sequential/#fit_generator

model.fit( x=x_train, y=y_train, batch_size=32, epochs=10, validation_data=(x_test, y_test), )

And I got this error : AttributeError: 'str' object has no attribute 'ndim'

At first, at the other github, they said I have to preprocess the data for training using this code:

import cv2 import numpy as np

ann_img = np.zeros((30,30,3)).astype('uint8') ann_img[ 3 , 4 ] = 1 # this would set the label of pixel 3,4 as 1

cv2.imwrite( "ann_1.png" ,ann_img ) -> But I dont know how to apply to in my total code

Please help me !!!

qubvel commented 5 years ago

Take some courses, you have to improve your python skills at least. Every time you create a new model by defining
model = ... And all previous models are discarded. You have to take kwargs that suit to your case and define model just once, in README there are three different independent examples (one for each particular case).