From 08fcafe5695af5fc6e642741df6e637e446c9e9b Mon Sep 17 00:00:00 2001 From: Pedro Cuenca Date: Fri, 21 Apr 2017 13:04:38 +0200 Subject: [PATCH 1/3] * Normalize images read from the filesystem so the floats are in the [0,1] range. I _believe_ this is the expected range for the VGG16 model. A better solution could be to perform a specific normalization step depending on the initial type and depth of the source image. This change is sufficient for me for now, as it allows the testing of regular JPG files whose ranges were in the [0, 255] range. --- 3. O'Reilly Generate from image.ipynb | 1 + 1 file changed, 1 insertion(+) diff --git a/3. O'Reilly Generate from image.ipynb b/3. O'Reilly Generate from image.ipynb index 7d1b1da..186255f 100644 --- a/3. O'Reilly Generate from image.ipynb +++ b/3. O'Reilly Generate from image.ipynb @@ -287,6 +287,7 @@ " image = cv2.imread(x)\n", " if as_float:\n", " image = image.astype(np.float32)\n", + " image = image / image.max()\n", "\n", " if len(image.shape) == 2:\n", " image = np.tile(image[:,:,None], 3)\n", From 6075e9e98da2cc1fa5b069579e49428e2e6b7250 Mon Sep 17 00:00:00 2001 From: Pedro Cuenca Date: Fri, 21 Apr 2017 13:16:22 +0200 Subject: [PATCH 2/3] * `test` function defaults to using a valid image path. --- 3. O'Reilly Generate from image.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/3. O'Reilly Generate from image.ipynb b/3. O'Reilly Generate from image.ipynb index 186255f..e0b9661 100644 --- a/3. O'Reilly Generate from image.ipynb +++ b/3. O'Reilly Generate from image.ipynb @@ -352,7 +352,7 @@ }, "outputs": [], "source": [ - "def test(sess,image,generated_words,ixtoword,test_image_path=0): # Naive greedy search\n", + "def test(sess,image,generated_words,ixtoword,test_image_path=image_path): # Naive greedy search\n", "\n", " \n", "\n", From 83330ab75bb9a055db878a3a97b54678401b84f9 Mon Sep 17 00:00:00 2001 From: Pedro Cuenca Date: Fri, 21 Apr 2017 13:18:23 +0200 Subject: [PATCH 3/3] * Use all the generated words if no period is part of the list. This was an usual occurrence while testing early epoch models. If the punctuation sign was not among the generated words, the `test` function only showed the first word ('a', usually). --- 3. O'Reilly Generate from image.ipynb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/3. O'Reilly Generate from image.ipynb b/3. O'Reilly Generate from image.ipynb index e0b9661..df5810f 100644 --- a/3. O'Reilly Generate from image.ipynb +++ b/3. O'Reilly Generate from image.ipynb @@ -371,9 +371,11 @@ " generated_word_index= sess.run(generated_words, feed_dict={image:fc7})\n", " generated_word_index = np.hstack(generated_word_index)\n", " generated_words = [ixtoword[x] for x in generated_word_index]\n", - " punctuation = np.argmax(np.array(generated_words) == '.')+1\n", - "\n", - " generated_words = generated_words[:punctuation]\n", + " \n", + " # Select words until the first period, or all words if there is no period\n", + " punctuation = np.argmax(np.array(generated_words) == '.')\n", + " if punctuation == 0: punctuation = len(generated_words)\n", + " generated_words = generated_words[:punctuation+1]\n", " generated_sentence = ' '.join(generated_words)\n", " print(generated_sentence)" ]