Exemplo n.º 1
0
def loadled():
    path = "/Users/wzq/Desktop/led/train"
    #dirs=os.listdir(path)
    label = []
    image = []
    for i in range(1, 10):

        dir_image = os.listdir(path + "/" + str(i))
        for j in dir_image:
            if j in [".DS_Store"]:
                continue
            gray = cv2.imread(path + "/" + str(i) + "/" + j,
                              cv2.IMREAD_GRAYSCALE)
            bw = cv2.adaptiveThreshold(gray, 255,
                                       cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                       cv2.THRESH_BINARY, 25, 25)
            bw = resize(bw)
            # cv2.imshow("",bw)
            # cv2.waitKey(0)
            # gray = cv2.resize(img, (28, 28), interpolation=cv2.INTER_AREA)
            # gray = gray[:, :, 0]
            # fd = hog(bw.reshape((28, 28)), orientations=9, pixels_per_cell=(1, 1), cells_per_block=(1, 1),
            #          visualise=False)
            label.append(i)
            image.append(bw)
    return image, label
Exemplo n.º 2
0
def create_training_data():
    image = []
    for person in Person:
        path = os.path.join(file, person)
        class_num = Person.index(person)
        for img in os.listdir(path):
            img_array = cv2.imread(os.path.join(path, img))
            new_array = cv2.resize(img_array, (224, 224))
            image.append([new_array, class_num])

    return image
data = pd.read_csv('mapping.csv')     # reading the csv file
data.head()      # printing first five rows of the file

X = [ ]     # creating an empty array
for img_name in data.Image_ID:
    img = plt.imread('' + img_name)
    X.append(img)  # storing each image in array X
X = np.array(X)    # converting list to array

y = data.Class
dummy_y = np_utils.to_categorical(y)    # one hot encoding Classes

image = []
for i in range(0,X.shape[0]):
    a = resize(X[i], preserve_range=True, output_shape=(224,224)).astype(int)      # reshaping to 224*224*3
    image.append(a)
X = np.array(image)

from keras.applications.vgg16 import preprocess_input
X = preprocess_input(X, mode='tf')      # preprocessing the input data

from sklearn.model_selection import train_test_split
X_train, X_valid, y_train, y_valid = train_test_split(X, dummy_y, test_size=0.3, random_state=42)    # preparing the validation set


from keras.models import Sequential
from keras.applications.vgg16 import VGG16
from keras.layers import Dense, InputLayer, Dropout


Exemplo n.º 4
0
https://www.kaggle.com/rhammell/ships-in-satellite-imagery
* initial attempt to classify based on the given lables (partial ship images classfied as '0')
* I made the dataset more complicated by reclassifying partial ships as '1' label; note that ship images crossing the corner
  were typically classified as '0' unless it's very clear the image is a ship (typically needs to include a bow or a stern)
* poor initial results addressed by building more data for misclassified types of images
* increased size of model to address the increased image complexity
* 91%+ prediction on the test set and can be further improved
'''


np.random.seed(88)

#upload RGB images
image = []
for image_path in glob.glob("/Users/vlad/Projects/ships-in-satellite-imagery/shipsnet/*.png"):
    image.append(misc.imread(image_path))
X_orig = np.asarray(image)

#get list of file names
filename = []
filename.append(glob.glob("/Users/vlad/Projects/ships-in-satellite-imagery/shipsnet/*.png"))
filename = np.asarray(filename).T

#60th position in line corresponds to the label value => create labels list
y_orig = []
for n in range(len(filename)):
    line = np.array_str(filename[n])
    y_orig.append(line[60])
y_orig = np.asarray(y_orig).T
y_orig = y_orig.reshape((len(y_orig), 1))
Exemplo n.º 5
0
def my_input(url):
    label_binarizer_classes_ = [
        'Pepper__bell___Bacterial_spot', 'Pepper__bell___healthy',
        'Potato___Early_blight', 'Potato___Late_blight', 'Potato___healthy',
        'Tomato_Bacterial_spot', 'Tomato_Early_blight', 'Tomato_Late_blight',
        'Tomato_Leaf_Mold', 'Tomato_Septoria_leaf_spot',
        'Tomato_Spider_mites_Two_spotted_spider_mite', 'Tomato__Target_Spot',
        'Tomato__Tomato_YellowLeaf__Curl_Virus', 'Tomato__Tomato_mosaic_virus',
        'Tomato_healthy'
    ]
    model = Sequential()
    inputShape = (256, 256, 3)
    chanDim = -1
    if K.image_data_format() == "channels_first":
        inputShape = (3, 256, 256)
        chanDim = 1
    model.add(Conv2D(32, (3, 3), padding="same", input_shape=inputShape))
    model.add(Activation("relu"))
    model.add(BatchNormalization(axis=chanDim))
    model.add(MaxPooling2D(pool_size=(3, 3)))
    model.add(Dropout(0.25))
    model.add(Conv2D(64, (3, 3), padding="same"))
    model.add(Activation("relu"))
    model.add(BatchNormalization(axis=chanDim))
    model.add(Conv2D(64, (3, 3), padding="same"))
    model.add(Activation("relu"))
    model.add(BatchNormalization(axis=chanDim))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))
    model.add(Conv2D(128, (3, 3), padding="same"))
    model.add(Activation("relu"))
    model.add(BatchNormalization(axis=chanDim))
    model.add(Conv2D(128, (3, 3), padding="same"))
    model.add(Activation("relu"))
    model.add(BatchNormalization(axis=chanDim))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))
    model.add(Flatten())
    model.add(Dense(1024))
    model.add(Activation("relu"))
    model.add(BatchNormalization())
    model.add(Dropout(0.5))
    model.add(Dense(15))
    model.add(Activation("softmax"))

    opt = Adam(lr=1e-3, decay=1e-3 / 25)
    model.compile(loss="binary_crossentropy",
                  optimizer=opt,
                  metrics=["accuracy"])

    model.load_weights(
        "C:/Users/Rajan Sethi/Desktop/Online Courses/mangobite/mangobite/weights_test_cnn.hdfs"
    )

    image = list()
    test = convert_image_to_array(url)
    image.append(test)
    np_image_test = np.array(image, dtype=np.float16) / 225.0
    predictions_1 = model.predict(np_image_test)

    predictions = np.argmax(predictions_1, 1)
    predictions[0]
    tf.keras.backend.clear_session()
    K.clear_session()
    return label_binarizer_classes_[predictions[0]]