5

My code:

    from __future__ import absolute_import, division, print_function, unicode_literals
from gensim.corpora import Dictionary
from tensorflow import keras


dictionary = Dictionary.load_from_text('diccionario_gensim.txt')

import spacy
import tensorflow as tf
import keras

import tensorflow as tf
import pandas as pd
import numpy as np
import os

def clean_up(text):
    removal=['ADV','PRON','CCONJ','PUNCT','PART','DET','ADP','SPACE']
    text_out = []
    doc= nlp(text)
    for token in doc:
        if token.is_stop == False and token.is_alpha and len(token)>1 and token.pos_ not in removal:
            lemma = token.lemma_
            text_out.append(lemma)
    return text_out

def procesarString (s, s2):
    text =  [dictionary.doc2idx(clean_up(s)), dictionary.doc2idx(clean_up(s2))]
    train_data = keras.preprocessing.sequence.pad_sequences(text,
                                                        value=0,
                                                        padding='post',
                                                        maxlen=512*2)
    return train_data

nlp = spacy.load("es_core_news_sm")

def create_model( ):
    m = keras.Sequential()
    m.add(keras.layers.Embedding(len (dictionary), 16))
    m.add(keras.layers.GlobalAveragePooling1D())
    m.add(keras.layers.Dense(16, activation=tf.nn.relu))
    m.add(keras.layers.Flatten(input_shape=(1024, )))
    m.add(keras.layers.Dense(25, activation='softmax'))
    m.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['acc'])
    return m

def predict(text):
    clean_up(text)
    procesarString(text, text)
    model2 = create_model()
    checkpoint_path = "training_1/cp.ckpt"
    checkpoint_dir = os.path.dirname(checkpoint_path)
    model2.load_weights(checkpoint_path)
    tags = ['Kit Cocina', 'Gastos notariales y de documentación', 'Prorroga Alojamiento temporal - Arriendo',
            'Kit Dormitorio', 'Gastos de atención en salud', 'Egreso de hotel', 'Visitas para arrendamiento',
            'Unidades de redención Alimentación - Aseo', 'Vestuario', 'Servicios funerarios',
            'Kit de vivienda saludable ', 'Transporte emergencia', 'Remisión albergue', 'Remision de hotel',
            'Orientación oferta distrital', 'Arriendo', 'Prórroga de hotel', 'Alojamiento temporal - Arriendo',
            'Remisión Alojamiento temporal - Albergue', 'Prórroga de arriendo',
            'Egreso Alojamiento temporal - Albergue', 'Transporte intraUrbano', 'Transporte', 'Kit Vajilla',
            'Kit de aseo personal']
    prediction = model2.predict(x[:1])
    ordered =  ( [(prediction[i], tags[i]) for i in range(len(prediction))])
    return ordered.Take(3)

I am getting the error in the title:

Input 0 is incompatible with layer flatten_1: expected min_ndim=3, found ndim=2

If I run the code on a Jupyter notebook it works, but I am migrating it to a Django app inside a docker container.

I put the same version of all the libraries inside the docker, but can't make it to work.

This is the stacktrace

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/series/modelPredict

Django Version: 2.2.1
Python Version: 3.7.3
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'rest_framework',
 'series']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/usr/local/lib/python3.7/site-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/usr/local/lib/python3.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
  54.         return view_func(*args, **kwargs)

File "/usr/src/app/series/views.py" in modelPredict
  56.     return model.predict("Que visaje la vida parce....")

File "/usr/src/app/series/model.py" in predict
  48.     model2 = create_model()

File "/usr/src/app/series/model.py" in create_model
  39.     m.add(keras.layers.Flatten(input_shape=(784, )))

File "/usr/local/lib/python3.7/site-packages/keras/engine/sequential.py" in add
  181.             output_tensor = layer(self.outputs[0])

File "/usr/local/lib/python3.7/site-packages/keras/engine/base_layer.py" in __call__
  414.                 self.assert_input_compatibility(inputs)

File "/usr/local/lib/python3.7/site-packages/keras/engine/base_layer.py" in assert_input_compatibility
  327.                                      str(K.ndim(x)))

Exception Type: ValueError at /series/modelPredict
Exception Value: Input 0 is incompatible with layer flatten_1: expected min_ndim=3, found ndim=2

Any help would be greatly appreciated.


Thank you all for your answers, unfortunately the project take a different approach, so I can't try this anymore, I will close the question.

Dawny33
  • 8,296
  • 12
  • 48
  • 104
Morgoth
  • 51
  • 1
  • 1
  • 4

1 Answers1

1

I got a similar problem solved while using transfer learing for CNNs by changing

base_model= VGG16(
    weights=('imagenet', include_top=False, input_shape = (128,128,3) )

to:

base_model= VGG16(
    weights=('imagenet', include_top=False, input_shape = [128,128,3] )

So seems like the type of bracket is important as well here with the input shape.

  • Thank you for your response, unfortunately the project take a different approach, so I can't try this anymore, I will close the question. – Morgoth Sep 30 '19 at 15:31