zhixuhao / unet

unet for image segmentation
MIT License
4.5k stars 1.99k forks source link

这是什么错误 #46

Open cbb0220 opened 6 years ago

cbb0220 commented 6 years ago

Using TensorFlow backend. Traceback (most recent call last): File "main.py", line 17, in model = unet() File "/home/chen/unet-master/model.py", line 36, in unet merge6 = merge([drop4,up6], mode = 'concat', concat_axis = 3) TypeError: 'module' object is not callable

ajithvcoder commented 6 years ago

try to execute trainUnet.ipyb with path inside unet folder

you can check the path with following commands import os cwd = os.getcwd() print(cwd) #....\unet

cbb0220 commented 6 years ago

I have checked the path ,can't found any problem. main path :/home/chen/unet-master model pth : /home/chen/unet-master

ajithvcoder commented 6 years ago

@cbb0220 which file you are executing ?

ajithvcoder commented 6 years ago

This is what i did

  1. i cloned the repository
    !git clone https://github.com/zhixuhao/unet

  2. went to command prompt gave path to ../../unet
    >cd '..\..\..\..\..\..\..\unet

  3. ran jupter notebook from command prompt >jupyter notebook

  4. clicked trainUnet.ipyb so it opened a notebook ran each cell

ajithvcoder commented 6 years ago

@cbb0220 did it work?

Minamiyama commented 6 years ago

if you use higher version of keras, you can add this def in model.py

def merge(inputs, mode, concat_axis=-1): return concatenate(inputs, concat_axis)

ajithvcoder commented 6 years ago

you can also replace al the merge layers like this. merge6 = merge([drop4,up6], mode = 'concat', concat_axis = 3)

with

merge6 = concatenate([drop4,up6],axis=3)

joanneong commented 6 years ago

After resolving that issue, I get another problem:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-31-8261be2be7e1> in <module>()
      7                     fill_mode='nearest')
      8 myGene = trainGenerator(2,'data/membrane/train','image','label',data_gen_args,save_to_dir = None)
----> 9 model = unet()
     10 model_checkpoint = ModelCheckpoint('unet_membrane.hdf5', monitor='loss',verbose=1, save_best_only=True)
     11 model.fit_generator(myGene,steps_per_epoch=2000,epochs=5,callbacks=[model_checkpoint])

/media/ath3/jo/ML and DL/Keras + Tensorflow/Unet/unet-master/model.py in unet(pretrained_weights, input_size)
     32     drop5 = Dropout(0.5)(conv5)
     33 
---> 34     up6 = Conv2D(512, 2, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(UpSampling2D(size = (2,2))(drop5))
     35     merge6 = concatenate([drop4,up6], axis = 3)
     36     conv6 = Conv2D(512, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(merge6)

TypeError: 'module' object is not callable

In attempting to resolve this as well, I have tried the following: 1) check that the functions are still in use and correctly employed here according to the Keras documentation

2) do from keras.layers import Conv2D, MaxPooling2D, UpSampling2D, Dropout instead of the given from keras.layers import *

3) tried doing model.add(Conv2D ...) instead of Conv2D...

but none of these methods worked.

Any ideas on how else to fix this? My python version is 3.5.2, and the keras I have installed is 2.2.0. Thanks!

ajithvcoder commented 6 years ago

@joanneong
I will check that issue . could you try below code

from up6 layer to conv 10 you need to provide the following code

up6 = Conv2DTranspose(512, (2,2),strides=(2,2), activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(drop5) merge6 = concatenate([drop4,up6],axis=3) conv6 = Conv2D(512, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(merge6) conv6 = Conv2D(512, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv6)

up7 = Conv2DTranspose(256,(2,2),strides=(2,2), activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv6) merge7 =concatenate([conv3,up7],axis=3) conv7 = Conv2D(256, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(merge7) conv7 = Conv2D(256, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv7)

up8 = Conv2DTranspose(128,(2,2),strides=(2,2), activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv7) merge8 =concatenate([conv2,up8],axis=3) conv8 = Conv2D(128, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(merge8) conv8 = Conv2D(128, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv8)

up9 = Conv2DTranspose(64,(2,2),strides=(2,2), activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv8) merge9 =concatenate([conv1,up9],axis=3) conv9 = Conv2D(64, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(merge9) conv9 = Conv2D(64, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv9) conv9 = Conv2D(2, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv9) conv10 = Conv2D(1, 1, activation = 'sigmoid')(conv9)

joanneong commented 6 years ago

@ajithvallabai The same error arises:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-42-8261be2be7e1> in <module>()
      7                     fill_mode='nearest')
      8 myGene = trainGenerator(2,'data/membrane/train','image','label',data_gen_args,save_to_dir = None)
----> 9 model = unet()
     10 model_checkpoint = ModelCheckpoint('unet_membrane.hdf5', monitor='loss',verbose=1, save_best_only=True)
     11 model.fit_generator(myGene,steps_per_epoch=2000,epochs=5,callbacks=[model_checkpoint])

/media/ath3/jo/ML and DL/Keras + Tensorflow/Unet/unet-master/model.py in unet(pretrained_weights, input_size)
     32     conv5 = Conv2D(1024, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv5)
     33     drop5 = Dropout(0.5)(conv5)
---> 34 
     35     up6 = Conv2DTranspose(512, (2,2),strides=(2,2), activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(drop5)
     36     merge6 = concatenate([drop4,up6],axis=3)

TypeError: 'module' object is not callable

Hmm...

ajithvcoder commented 6 years ago

@joanneong
i think you have forgot concatenate from keras.layers import Conv2D, MaxPooling2D, UpSampling2D, Dropout,Conv2DTranspose,concatenate

joanneong commented 6 years ago

@ajithvallabai Forgot to mention this, but I am already doing from keras.layers.merge import concatenate.

If it helps, here is my model.py right now:

import numpy as np 
import os
import skimage.io as io
import skimage.transform as trans
import numpy as np
from keras.models import *
from keras.layers import MaxPooling2D, UpSampling2D, Dropout
from keras.layers.convolutional import Conv2D, Conv2DTranspose
from keras.layers.merge import concatenate
from keras.optimizers import *
from keras.callbacks import ModelCheckpoint, LearningRateScheduler
from keras import backend as keras

def unet(pretrained_weights = None,input_size = (256,256,1)):
    inputs = Input(input_size)
    conv1 = model.add(Conv2D(64, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(inputs))
    conv1 = Conv2D(64, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv1)
    pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
    conv2 = Conv2D(128, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(pool1)
    conv2 = Conv2D(128, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv2)
    pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
    conv3 = Conv2D(256, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(pool2)
    conv3 = Conv2D(256, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv3)
    pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)
    conv4 = Conv2D(512, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(pool3)
    conv4 = Conv2D(512, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv4)
    drop4 = Dropout(0.5)(conv4)
    pool4 = MaxPooling2D(pool_size=(2, 2))(drop4)

    conv5 = Conv2D(1024, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(pool4)
    conv5 = Conv2D(1024, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv5)
    drop5 = Dropout(0.5)(conv5)

    up6 = Conv2DTranspose(512, (2,2),strides=(2,2), activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(drop5)
    merge6 = concatenate([drop4,up6],axis=3)
    conv6 = Conv2D(512, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(merge6)
    conv6 = Conv2D(512, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv6)

    up7 = Conv2DTranspose(256,(2,2),strides=(2,2), activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv6)
    merge7 =concatenate([conv3,up7],axis=3)
    conv7 = Conv2D(256, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(merge7)
    conv7 = Conv2D(256, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv7)

    up8 = Conv2DTranspose(128,(2,2),strides=(2,2), activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv7)
    merge8 =concatenate([conv2,up8],axis=3)
    conv8 = Conv2D(128, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(merge8)
    conv8 = Conv2D(128, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv8)

    up9 = Conv2DTranspose(64,(2,2),strides=(2,2), activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv8)
    merge9 =concatenate([conv1,up9],axis=3)
    conv9 = Conv2D(64, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(merge9)
    conv9 = Conv2D(64, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv9)
    conv9 = Conv2D(2, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv9)
    conv10 = Conv2D(1, 1, activation = 'sigmoid')(conv9)

    model = Model(input = inputs, output = conv10)

    model.compile(optimizer = Adam(lr = 1e-4), loss = 'binary_crossentropy', metrics = ['accuracy'])

    #model.summary()

    if(pretrained_weights):
        model.load_weights(pretrained_weights)

    return model

which unfortunately outputs the error message that I have posted above. Are you able to reproduce this problem?

ajithvcoder commented 6 years ago

I am also using python 3.5.2 but my keras version is 2.0.5 and its working .So i think its keras version making the problem.

i think this is the problem change input to inputs and output to outputs model = Model(inputs = inputs, outputs = conv10) it is the solution for below warning Update your Model call to the Keras 2 API: Model(inputs=Tensor("in..., outputs=Tensor("co...)

i get some warning may be it can help you

   UserWarning: Update your `Model` call to the Keras 2 API: `Model(inputs=Tensor("in..., 
   outputs=Tensor("co...)` 
   WARNING:tensorflow:From C:\Users\C5248734\AppData\Local\Programs\Python\Python35\Lib\site- 
   packages\keras\backend\tensorflow_backend.py:1297: calling reduce_mean (from 
   tensorflow.python.ops.math_ops) with keep_dims is deprecated and will be removed in a future 
   version.
   Instructions for updating:
   keep_dims is deprecated, use keepdims instead
Einsley commented 5 years ago

@ajithvallabai Hi, thanks for the code you provided above. I encountered the same problem saying 'module' object is not callable, but your revised code helped. However, I do not think I can get the 0.97 training accuracy as what the author got. I haven't completed training yet, but accuracy after 4 epochs was only 0.78 . What accuracy did you get? Thanks.

ajithvcoder commented 5 years ago

@Einsley Ya i too faced that problem 0.78 is a greyed out image .The below one solved that issue for me you check from your side In train.ipynb change steps_per_epoch to 300 or 400 or 700 or 1000 .Any one of them should work model.fit_generator(myGene,steps_per_epoch=2000,epochs=5,callbacks=[model_checkpoint]).

Also you can check by changing the initiliser from he_normal to some other and try i didnt try that .

alyato commented 5 years ago

@ajithvallabai thanks why it occur this question using merge,but will be normal using concatenate.

BarryBA commented 5 years ago

you can also replace al the merge layers like this. merge6 = merge([drop4,up6], mode = 'concat', concat_axis = 3)

with

merge6 = concatenate([drop4,up6],axis=3)

@ajithvallabai @Minamiyama Thank you for your advice. I wonder to know what the meaning of 'concat_axis=3' or 'axis=3'?

alyato commented 5 years ago

@BarryBA drop4 is (none,7,7,64) up6 is (none,7,7,32) when concat_axis=3, meaning that 64+32 it also be set concat_axis=-1.

parsboy66 commented 5 years ago

hey, I 'v seen your problem just now if u look at the first line in ur code 'model' imported as a subset of the main program and also few lines after that model is made as ur deep learning mode!!!!! just change the name of the model which u want to build to model1 for example....that's all

parsboy66 commented 5 years ago

like this data_gen_args = dict(rotation_range=0.2, width_shift_range=0.05, height_shift_range=0.05, shear_range=0.05, zoom_range=0.05, horizontal_flip=True, fill_mode='nearest') myGene = trainGenerator(2,'data/membrane/train','image','label',data_gen_args,save_to_dir = None) model1 = unet() model_checkpoint = ModelCheckpoint('unet_membrane.hdf5', monitor='loss',verbose=1, save_best_only=True) model1.fit_generator(myGene,steps_per_epoch=2000,epochs=5,callbacks=[model_checkpoint])

kl-31 commented 5 years ago

Hello, I also had this issue, which has been resolved with the following easy fix.

Change all Merge layers to the following: mergeN = Concatenate(axis=3)([tensor1,tensor2]) where N, tensor1, tensor2 are the relevant parameters There are 2 sets of parentheses. The first set contains the options, the second set contains the two input tensors to be concatenated. This implementation is consistent with that of all other layers.

The previous version Concatenate([tensor1,tensor2], axis=3) may have worked with previous versions of Keras, but it no longer works now (Keras 2.2.4).

Be careful to restart the jupyter kernel and re-import everything after each time that you tweak something in the model.py file.

Importing layers with * is fine. model = unet() is fine.

Iammuratc commented 5 years ago

@joanneong It is just because line 34 is cursed :) even if you make the line 34 blank, it shows the error there.

I could not find any solution neither, despite I try all of the suggestions above.

Can anyone share a working code ?(Keras 2.2.4)(Python 3.6.6)

Edit: It just worked right after I restart the kernel !!!Yay!

tynefung commented 5 years ago

add

def merge(inputs, mode, concat_axis=-1):
    return Concatenate(concat_axis)(inputs)

to model.py before def unet

if you are using notebook, remember to restart kernel.

vinushree95 commented 5 years ago

def unet(): inputs = Input((1,512, 512)) conv1 = Conv2D(width, 3, 3, activation='relu', border_mode='same')(inputs)

conv1 = BatchNormalization(axis = 1)(conv1)
conv1 = Conv2D(width, 3, 3, activation='relu', border_mode='same')(conv1)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)

conv2 = Conv2D(width*2, 3, 3, activation='relu', border_mode='same')(pool1)
conv2 = BatchNormalization(axis = 1)(conv2)
conv2 = Conv2D(width*2, 3, 3, activation='relu', border_mode='same')(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)

conv3 = Conv2D(width*4, 3, 3, activation='relu', border_mode='same')(pool2)
conv3 = BatchNormalization(axis = 1)(conv3)
conv3 = Conv2D(width*4, 3, 3, activation='relu', border_mode='same')(conv3)
pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)

conv4 = Conv2D(width*8, 3, 3, activation='relu', border_mode='same')(pool3)
conv4 = BatchNormalization(axis = 1)(conv4)
conv4 = Conv2D(width*8, 3, 3, activation='relu', border_mode='same')(conv4)
pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)

conv5 = Conv2D(width*16, 3, 3, activation='relu', border_mode='same')(pool4)
conv5 = BatchNormalization(axis = 1)(conv5)
conv5 = Conv2D(width*16, 3, 3, activation='relu', border_mode='same')(conv5)

up6 = merge([UpSampling2D(size=(2, 2))(conv5), conv4], mode='concat', concat_axis=1)
conv6 = SpatialDropout2D(0.35)(up6)
conv6 = Conv2D(width*8, 3, 3, activation='relu', border_mode='same')(conv6)
conv6 = Conv2D(width*8, 3, 3, activation='relu', border_mode='same')(conv6)

up7 = merge([UpSampling2D(size=(2, 2))(conv6), conv3], mode='concat', concat_axis=1)
conv7 = SpatialDropout2D(0.35)(up7)
conv7 = Conv2D(width*4, 3, 3, activation='relu', border_mode='same')(conv7)
conv7 = Conv2D(width*4, 3, 3, activation='relu', border_mode='same')(conv7)

up8 = merge([UpSampling2D(size=(2, 2))(conv7), conv2], mode='concat', concat_axis=1)
conv8 = SpatialDropout2D(0.35)(up8)
conv8 = Conv2D(width*2, 3, 3, activation='relu', border_mode='same')(conv8)
conv8 = Conv2D(width*2, 3, 3, activation='relu', border_mode='same')(conv8)

up9 = merge([UpSampling2D(size=(2, 2))(conv8), conv1], mode='concat', concat_axis=1)
conv9 = SpatialDropout2D(0.35)(up9)
conv9 = Conv2D(width, 3, 3, activation='relu', border_mode='same')(conv9)
conv9 = Conv2D(width, 3, 3, activation='relu', border_mode='same')(conv9)
conv10 = Conv2D(1, 1, 1, activation='sigmoid')(conv9)

model = Model(input=inputs, output=conv10)
model.compile(optimizer=Adam(lr=1e-5), loss=dice_coef_loss, metrics=[dice_coef])
return model

Error:

in unet() 38 conv5 = Conv2D(width*16, 3, 3, activation='relu', border_mode='same')(conv5) 39 ---> 40 up6 = merge([UpSampling2D(size=(2, 2))(conv5), conv4], mode='concat', concat_axis=1) 41 conv6 = SpatialDropout2D(0.35)(up6) 42 conv6 = Conv2D(width*8, 3, 3, activation='relu', border_mode='same')(conv6) TypeError: 'module' object is not callable