0

I have a list which is 2 dimensional and has elements like ([0,1,2],[3,4,5]). Type of its elements is numpy.ndarray. I am trying to delete 2nd columns of each element. When I check its type, it returns list but it gives ValueError: cannot delete array elements error. I checked StackOverflow but haven't found a similar case. The code is below, any help is appreciated.

for row in trainSet:
    del row[1]  
6
  • Does this answer your question? Deleting Elements from an array Commented Nov 22, 2021 at 19:10
  • Have you checked the type of the subarrays within your list? Commented Nov 22, 2021 at 19:10
  • No, as I mentioned I use list not array. Commented Nov 22, 2021 at 19:10
  • my list consists of 'numpy.ndarray' I just realized that. So how can I delete that column, I am still confused Commented Nov 22, 2021 at 19:11
  • It is already a list, I was just trying to check. Commented Nov 22, 2021 at 19:12

2 Answers 2

1

Its a list that contains numpy arrays

import numpy as np
trainSet = np.array([[0,1,2],[3,4,5]]) 


#wrong way list(trainSet) # this do not convert nested lists
trainSet = list(map(lambda x: x.tolist(),trainSet)) #to make sure it is a list, do not contain nested numpy arrays
for row in trainSet:
    del row[1] 

print(trainSet)

[[0, 2], [3, 5]]
Sign up to request clarification or add additional context in comments.

Comments

-2
import numpy as np
import pandas as pd
import os
for dirname, _, filenames in os.walk('/datasets/train'):
    for filename in filenames:
        os.path.join(dirname, filename
import cv2
import matplotlib.pyplot as plt
import tensorflow as tf
from tqdm.notebook import tqdm
from glob import glob
from keras.layers import Flatten, Dense
from keras.applications.vgg16 import VGG16
import xml.etree.ElementTree as xet
from keras.models import load_model, Sequential
from sklearn.model_selection import train_test_split
def load_images(directory):
    images = []

    for filepath in tqdm(os.listdir(directory)):
        img_path = os.path.join(directory,filepath)
        img = cv2.imread(img_path)
        img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
        img = cv2.resize(img,(224,224))
        img = cv2.medianBlur(img,5)
        images.append(img)
    
    images = np.array(images).reshape(len(images),224,224,3)
    return images
path = glob("./datasets/train/annotations/*.xml")
labels_dict = dict(filepath=[],xmin=[],xmax=[],ymin=[],ymax=[])

for filename in path:
    data = xet.parse(filename)
    root = data.getroot()
    obj = root.find('object')
    labels_info = obj.find('bndbox')
    xmin = int(labels_info.find('xmin').text)
    xmax = int(labels_info.find('xmax').text)
    ymin = int(labels_info.find('ymin').text)
    ymax = int(labels_info.find('ymax').text)
    labels_dict['filepath'].append(filename)
    labels_dict['xmin'].append(xmin)
    labels_dict['xmax'].append(xmax)
    labels_dict['ymin'].append(ymin)
    labels_dict['ymax'].append(ymax)
df = pd.DataFrame(labels_dict)
df.to_csv('labels.csv',index=False)
df.head()
images = load_images('./datasets/train/images/')
images.shape
filename = df['filepath'][0]
def getFilename(filename):
    filename_image = xet.parse(filename).getroot().find('filename').text
    filepath_image = os.path.join('./datasets/train/images/',filename_image)
    return filepath_image
getFilename(filename)
image_path=list(df['filepath'].apply(getFilename))
image_path[:10]
bbox_coords = df.iloc[:,1:].values
labels = []


for ind in range(len(image_path)):
    image = image_path[ind]
    img_arr = cv2.imread(image)
    h,w,d = img_arr.shape
    xmin,xmax,ymin,ymax = bbox_coords[ind]
    nxmin, nxmax = int(xmin/(w/255.)), int(xmax/(w/255.))
    nymin, nymax = int(ymin/(h/255.)), int(ymax/(h/255.))
    labels.append((nxmin,nxmax,nymin,nymax))
labels = np.array(labels)
labels

X = np.array(images,dtype=np.float32)
y = np.array(labels,dtype=np.float32)

X = X / 255.
y = y / 255.

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=42)

X_train, X_val, y_train, y_val = train_test_split(X_train,y_train,test_size=0.1,random_state=42)

model = Sequential()
model.add(VGG16(weights="imagenet", include_top=False, input_shape=(224, 224, 3)))
model.add(Flatten())
model.add(Dense(128, activation="relu"))
model.add(Dense(128, activation="relu"))
model.add(Dense(64, activation="relu"))
model.add(Dense(4, activation="sigmoid"))

model.layers[-6].trainable = False

model.summary()

model.compile(loss='mean_squared_error', optimizer='adam', metrics=['accuracy'])

r = model.fit(X_train,
              y_train,
              epochs=10,
              batch_size=32,
              validation_data= 
              (X_val,y_val),
              verbose=1)

model.save('license_plate_detector.h5')

1 Comment

This answer has nothing to do with the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.