简体   繁体   English

Python,Keras,Tensorflow

[英]Python, Keras,Tensorflow

I'm trying to load in data for a school project on Tensorflow/Keras. 我正在尝试为Tensorflow / Keras上的学校项目加载数据。

import numpy as np
import matplotlib.pyplot as plt
import os
import cv2
import json

DATADIR = (r"C:\Users\ellio\Anaconda Lessons & Quizzes\Competition\Images")
with open('categories.json') as json_file:  
    categories = json.load(json_file)

for category in categories:
    path=os.path.join(DATADIR,category)
    for img in os.listdir(path):
        img_array=cv2.imread(os.path.join(path,img),cv2.IMREAD_GRAYSCALE)
        plt.imshow(img_array,cmap='gray')
        plt.show()
        break
break

training_data = []

def create_training_data(): 
    for category in categories:
        path=os.path.join(DATADIR,category)
        class_num=categories.index(category)
        for img in os.listdir(path):
            try:
                img_array=cv2.imread(os.path.join(path,img),cv2.IMREAD_GRAYSCALE)
                new_array=cv2.resize(img_array,(IMG_SIZE,IMG_SIZE))
                training_data.append([new_array,class_num])
            except Exception as e:
                pass

create_training_data()

When I enter the code, the error is 'dict' object has no attribute 'index' 当我输入代码时,错误是'dict' object has no attribute 'index'

I reckon this has something to do with my json file on the categories. 我认为这与我在类别上的json文件有关。 Below are are the categories from the json file. 以下是json文件中的类别。 Is something to do with it being a dictionary instead of a list? 与它是字典而不是列表有关吗?

{'Mobile': {'Others Mobile & Tablet': 35, 'Smartfren': 53, 'Infinix': 40, 'Brandcode': 39, 'Icherry': 52, 'Advan': 45, 'Iphone': 31, 'Realme': 51, 'Motorola': 49, 'Maxtron': 56, 'Nokia': 38, 'Xiaomi': 34, 'Mito': 46, 'Sony': 33, 'SPC': 57, 'Lenovo': 37, 'Alcatel': 55, 'Samsung': 32, 'Vivo': 42, 'Evercoss': 44, 'Strawberry': 50, 'Blackberry': 36, 'Asus': 43, 'Honor': 54, 'Oppo': 41, 'Huawei': 47, 'Sharp': 48}, 'Fashion': {'Wedding Dress': 23, 'Shirt': 27, 'Casual Dress': 18, 'Maxi Dress': 20, 'Big Size Dress': 24, 'Bodycon Dress': 22, 'Party Dress': 19, 'Blouse\xa0': 26, 'Tshirt': 25, 'Crop Top ': 29, 'Tanktop': 28, 'Others': 17, 'A Line Dress': 21, 'Big Size Top': 30}, 'Beauty': {'Foundation': 1, 'Face Palette': 0, 'Concealer': 7, 'Lip Gloss': 14, 'Blush On': 2, 'Highlighter': 8, 'BB & CC Cream': 5, 'Other Face Cosmetics': 4, 'Lip Tint': 13, 'Bronzer': 11, 'Lip Liner': 15, 'Powder': 3, 'Setting Spray': 10, 'Primer': 9, 'Contour': 6, 'Other Lip Cosmetics': 16, 'Lipstick': 12}}

Hey first of all I would avoid having spaces in directories. 嘿,首先我会避免在目录中有空格。

DATADIR = (r"C:\Users\ellio\Anaconda Lessons & Quizzes\Competition\Images")

Also, in python, the value of a dictionary object is based off a key rather than an index therefore it should be: 此外,在python中,字典对象的值基于键而不是索引,因此它应该是:

class_num=categories.get(category)

or 要么

class_num=categories[category]

check here for more: https://docs.python.org/2/tutorial/datastructures.html#dictionaries 点击此处查看更多信息: https//docs.python.org/2/tutorial/datastructures.html#dictionaries

In your case (nested json structure), without flattening your json you would not get what you want. 在你的情况下(嵌套的json结构),没有展平你的json你就不会得到你想要的。

You should flatten dictionary at first by creating a function: 你应该首先通过创建一个函数来展平字典:

def flatten(d):
    if not isinstance(d, dict):
        return d
    flattened = dict()
    for k in d:
        if isinstance(d[k], dict):
            flattened = {**flattened, **flatten(d[k])}
        else:
            flattened[k] = d[k]
    return flattened

And add this line after your json.load line: 并在json.load行之后添加以下行:

categories = flatten(categories)

Finally you will get your class_num in your for loop by: 最后,您将通过以下方式获取for循环中的class_num

class_num = categories[category]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM