How to feed in images into the model Keras











up vote
0
down vote

favorite












I have a dataset for the self-driving car. My X values are the names of the images. Example would be



array([['img_2.png'],
['img_3.png'],
['img_4.png'],
...,
['img_6405.png'],
['img_6406.png'],
['img_6407.png']], dtype=object)


I found out that model performs good if we have some kind of batch_generator. I found that template code.



def batch_generator(image_paths, steering_ang, batch_size, istraining):

while True:
batch_img =
batch_steering =

for i in range(batch_size):
random_index = random.randint(0, len(image_paths) - 1)

if istraining:
im = random_augment(image_paths[random_index])
steering = steering_ang[random_index]
else:
im = mpimg.imread(image_paths[random_index])
steering = steering_ang[random_index]

im = img_preprocess(im)
batch_img.append(im)
batch_steering.append(steering)
yield (np.asarray(batch_img), np.asarray(batch_steering))


I changed this function to for my use but when i apply it.



x_train_gen, y_train_gen = next(batch_generator(X_train, y_train, 1, 1))
x_valid_gen, y_valid_gen = next(batch_generator(X_valid, y_valid, 1, 0))


I get the following error TypeError: Object does not appear to be a 8-bit string path or a Python file-like object. I understand the error, image is not an array but a string. How can i convert string of the image path to the array










share|improve this question


























    up vote
    0
    down vote

    favorite












    I have a dataset for the self-driving car. My X values are the names of the images. Example would be



    array([['img_2.png'],
    ['img_3.png'],
    ['img_4.png'],
    ...,
    ['img_6405.png'],
    ['img_6406.png'],
    ['img_6407.png']], dtype=object)


    I found out that model performs good if we have some kind of batch_generator. I found that template code.



    def batch_generator(image_paths, steering_ang, batch_size, istraining):

    while True:
    batch_img =
    batch_steering =

    for i in range(batch_size):
    random_index = random.randint(0, len(image_paths) - 1)

    if istraining:
    im = random_augment(image_paths[random_index])
    steering = steering_ang[random_index]
    else:
    im = mpimg.imread(image_paths[random_index])
    steering = steering_ang[random_index]

    im = img_preprocess(im)
    batch_img.append(im)
    batch_steering.append(steering)
    yield (np.asarray(batch_img), np.asarray(batch_steering))


    I changed this function to for my use but when i apply it.



    x_train_gen, y_train_gen = next(batch_generator(X_train, y_train, 1, 1))
    x_valid_gen, y_valid_gen = next(batch_generator(X_valid, y_valid, 1, 0))


    I get the following error TypeError: Object does not appear to be a 8-bit string path or a Python file-like object. I understand the error, image is not an array but a string. How can i convert string of the image path to the array










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a dataset for the self-driving car. My X values are the names of the images. Example would be



      array([['img_2.png'],
      ['img_3.png'],
      ['img_4.png'],
      ...,
      ['img_6405.png'],
      ['img_6406.png'],
      ['img_6407.png']], dtype=object)


      I found out that model performs good if we have some kind of batch_generator. I found that template code.



      def batch_generator(image_paths, steering_ang, batch_size, istraining):

      while True:
      batch_img =
      batch_steering =

      for i in range(batch_size):
      random_index = random.randint(0, len(image_paths) - 1)

      if istraining:
      im = random_augment(image_paths[random_index])
      steering = steering_ang[random_index]
      else:
      im = mpimg.imread(image_paths[random_index])
      steering = steering_ang[random_index]

      im = img_preprocess(im)
      batch_img.append(im)
      batch_steering.append(steering)
      yield (np.asarray(batch_img), np.asarray(batch_steering))


      I changed this function to for my use but when i apply it.



      x_train_gen, y_train_gen = next(batch_generator(X_train, y_train, 1, 1))
      x_valid_gen, y_valid_gen = next(batch_generator(X_valid, y_valid, 1, 0))


      I get the following error TypeError: Object does not appear to be a 8-bit string path or a Python file-like object. I understand the error, image is not an array but a string. How can i convert string of the image path to the array










      share|improve this question













      I have a dataset for the self-driving car. My X values are the names of the images. Example would be



      array([['img_2.png'],
      ['img_3.png'],
      ['img_4.png'],
      ...,
      ['img_6405.png'],
      ['img_6406.png'],
      ['img_6407.png']], dtype=object)


      I found out that model performs good if we have some kind of batch_generator. I found that template code.



      def batch_generator(image_paths, steering_ang, batch_size, istraining):

      while True:
      batch_img =
      batch_steering =

      for i in range(batch_size):
      random_index = random.randint(0, len(image_paths) - 1)

      if istraining:
      im = random_augment(image_paths[random_index])
      steering = steering_ang[random_index]
      else:
      im = mpimg.imread(image_paths[random_index])
      steering = steering_ang[random_index]

      im = img_preprocess(im)
      batch_img.append(im)
      batch_steering.append(steering)
      yield (np.asarray(batch_img), np.asarray(batch_steering))


      I changed this function to for my use but when i apply it.



      x_train_gen, y_train_gen = next(batch_generator(X_train, y_train, 1, 1))
      x_valid_gen, y_valid_gen = next(batch_generator(X_valid, y_valid, 1, 0))


      I get the following error TypeError: Object does not appear to be a 8-bit string path or a Python file-like object. I understand the error, image is not an array but a string. How can i convert string of the image path to the array







      python image-processing keras neural-network deep-learning






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 8 at 2:46







      user9900027































          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          I don't know what you're doing in the img_preprocess() function but from what I see there are 2 possible problems:




          1. You have to append the path to the image to the image name: path_to_image = path_to_image_dir + '/' + image


          2. You have to actually open the image to get it's array. You can use Pillow or OpenCV:
            PIL.Image.open(path_to_image) or cv2.imread(path_to_image)







          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53200839%2fhow-to-feed-in-images-into-the-model-keras%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown
























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            I don't know what you're doing in the img_preprocess() function but from what I see there are 2 possible problems:




            1. You have to append the path to the image to the image name: path_to_image = path_to_image_dir + '/' + image


            2. You have to actually open the image to get it's array. You can use Pillow or OpenCV:
              PIL.Image.open(path_to_image) or cv2.imread(path_to_image)







            share|improve this answer

























              up vote
              0
              down vote













              I don't know what you're doing in the img_preprocess() function but from what I see there are 2 possible problems:




              1. You have to append the path to the image to the image name: path_to_image = path_to_image_dir + '/' + image


              2. You have to actually open the image to get it's array. You can use Pillow or OpenCV:
                PIL.Image.open(path_to_image) or cv2.imread(path_to_image)







              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                I don't know what you're doing in the img_preprocess() function but from what I see there are 2 possible problems:




                1. You have to append the path to the image to the image name: path_to_image = path_to_image_dir + '/' + image


                2. You have to actually open the image to get it's array. You can use Pillow or OpenCV:
                  PIL.Image.open(path_to_image) or cv2.imread(path_to_image)







                share|improve this answer












                I don't know what you're doing in the img_preprocess() function but from what I see there are 2 possible problems:




                1. You have to append the path to the image to the image name: path_to_image = path_to_image_dir + '/' + image


                2. You have to actually open the image to get it's array. You can use Pillow or OpenCV:
                  PIL.Image.open(path_to_image) or cv2.imread(path_to_image)








                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 8 at 8:01









                Novak

                67249




                67249






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53200839%2fhow-to-feed-in-images-into-the-model-keras%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Tangent Lines Diagram Along Smooth Curve

                    Yusuf al-Mu'taman ibn Hud

                    Zucchini