Value Error: Input shape error in Keras Image captioning

تعرفه تبلیغات در سایت
عنوان عکس
عنوان عکس
عنوان عکس
عنوان عکس
عنوان عکس


یافته ها در جستجو

    امکانات وب

    آرشیو مطالب

    برچسب ها

    Vote count: 0

    I'm new to Machine Learning and I've been working on this tutorial for sometime and it needs more than 45GB of RAM to run. So I've tried progressive Loading from this tutorial.

    Here is the error what i'm getting ValueError: Error when checking input: expected input_1 to have 2 dimensions, but got array with shape (13, 224, 224, 3)

    here the model function

    # define the captioning model
    def define_model(vocab_size, max_length):
        # feature extractor model
        inputs1 = Input(shape=(4096,))
        fe1 = Dropout(0.5)(inputs1)
        fe2 = Dense(256, activation='relu')(fe1)
        # sequence model
        inputs2 = Input(shape=(max_length,))
        se1 = Embedding(vocab_size, 256, mask_zero=True)(inputs2)
        se2 = Dropout(0.5)(se1)
        se3 = LSTM(256)(se2)
        # decoder model
        decoder1 = add([fe2, se3])
        decoder2 = Dense(256, activation='relu')(decoder1)
        outputs = Dense(vocab_size, activation='softmax')(decoder2)
        # tie it together [image, seq] [word]
        model = Model(inputs=[inputs1, inputs2], outputs=outputs)
        model.compile(loss='categorical_crossentropy', optimizer='adam')
        # summarize model
        plot_model(model, to_file='model.png', show_shapes=True)
        return model

    loading_photo function

    # load a single photo intended as input for the VGG feature extractor model
    def load_photo(filename):
        image = load_img(filename, target_size=(224, 224))
        # convert the image pixels to a numpy array
        image = img_to_array(image)
        # reshape data for the model
        image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
        # prepare the image for the VGG model
        image = preprocess_input(image)[0]
        # get image id
        image_id = filename.split('/')[-1].split('.')[0]
        return image, image_id

    create_sequences and data_generator functions

    # create sequences of images, input sequences and output words for an image
    def create_sequences(tokenizer, max_length, desc, image):
        Ximages, XSeq, y = list(), list(),list()
        vocab_size = len(tokenizer.word_index) + 1
        # integer encode the description
        seq = tokenizer.texts_to_sequences([desc])[0]
        # split one sequence into multiple X,y pairs
        for i in range(1, len(seq)):
            # select
            in_seq, out_seq = seq[:i], seq[i]
            # pad input sequence
            in_seq = pad_sequences([in_seq], maxlen=max_length)[0]
            # encode output sequence
            out_seq = to_categorical([out_seq], num_classes=vocab_size)[0]
            # store
        Ximages, XSeq, y = array(Ximages), array(XSeq), array(y)
        return [Ximages, XSeq, y]
    # data generator, intended to be used in a call to model.fit_generator()
    def data_generator(descriptions, tokenizer, max_length):
        # loop for ever over images
        directory = 'Flicker8k_Dataset'
        while 1:
            for name in listdir(directory):
                # load an image from file
                filename = directory + '/' + name
                image, image_id = load_photo(filename)
                # create word sequences
                desc = descriptions[image_id]
                in_img, in_seq, out_word = create_sequences(tokenizer, max_length, desc, image)
                yield [[in_img, in_seq], out_word]

    and finally

    model = define_model(vocab_size, max_length)
    # define checkpoint callback
    filepath = 'model-ep{epoch:03d}-loss{loss:.3f}-val_loss{val_loss:.3f}.h5'
    checkpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=1, save_best_only=True, mode='min')
    # fit model
    model.fit_generator(data_generator(descriptions, tokenizer, max_length), steps_per_epoch=70000)
    asked 1 min ago
    Vineeth Sai

    نویسنده : استخدام کار بازدید : 4 تاريخ : چهارشنبه 25 بهمن 1396 ساعت: 12:59
    برچسب‌ها :
    اخبار و رسانه هاهنر و ادبیاترایانه و اینترنتعلم و فن آوریتجارت و اقتصاداندیشه و مذهبفوتو بلاگوبلاگ و وبلاگ نویسیفرهنگ و تاریخجامعه و سیاستورزشسرگرمی و طنزشخصیخانواده و زندگیسفر و توریسمفارسی زبان در دیگر کشورها