WaqasSultani / AnomalyDetectionCVPR2018

502 stars 182 forks source link

AttributeError: 'Tensor' object has no attribute 'broadcastable' #88

Open lamees-hourani opened 2 years ago

lamees-hourani commented 2 years ago

I tried to copy the code in Kaggle platform to use but i keep facing this same error , any one know the solution ?

AttributeError: 'Tensor' object has no attribute 'broadcastable'

the error happens at train_on_batch

this is my code

model = Sequential()
model.add(Dense(512, input_dim=4096,kernel_initializer='glorot_normal',kernel_regularizer=l2(0.001),activation='relu'))
model.add(Dropout(0.6))
model.add(Dense(32,kernel_initializer='glorot_normal',kernel_regularizer=l2(0.001)))
model.add(Dropout(0.6))
model.add(Dense(1,kernel_initializer='glorot_normal',kernel_regularizer=l2(0.001),activation='sigmoid'))

adagrad=Adagrad(lr=0.01, epsilon=1e-08)

model.compile(loss=custom_objective, optimizer=adagrad)

print("Starting training...")

#AllClassPath='../input/anomalydetectiondatasetucf/Extracted C3D Features/Extracted C3D Features'
AllClassPath='../input/anomalydetectiondatasetucf/Extracted C3D Features/Extracted C3D Features'
# AllClassPath contains C3D features (.txt file)  of each video. Each text file contains 32 features, each of 4096 dimension
output_dir='/newdata/UCF_Anomaly_Dataset/Dataset/CVPR_Data/Trained_Models/TrainedModel_MIL_C3D/'
# Output_dir is the directory where you want to save trained weights
weights_path = output_dir + 'weights.mat'
# weights.mat are the model weights that you will get after (or during) that training
model_path = output_dir + 'model.json'

if not os.path.exists(output_dir): 
       os.makedirs(output_dir)

All_class_files= listdir(AllClassPath)
All_class_files.sort()
#print(All_class_files)

AbnormalPath_road = os.path.join(AllClassPath, All_class_files[2])

All_class_files_anomaly= listdir(AbnormalPath_road)
All_class_files_anomaly.sort()
#print(All_class_files_anomaly)

loss_graph =[]
num_iters = 20000
total_iterations = 0
batchsize=60
time_before = datetime.now()
print (keras.backend)
for it_num in range(num_iters):

    AbnormalPath = os.path.join(AbnormalPath_road, All_class_files_anomaly[0])  # Path of abnormal already computed C3D features
    NormalPath = os.path.join(AllClassPath, All_class_files[6])    # Path of Normal already computed C3D features
    inputs, targets=load_dataset_Train_batch(AbnormalPath, NormalPath)  # Load normal and abnormal video C3D features
    batch_loss =model.train_on_batch(inputs, targets)
    loss_graph = np.hstack((loss_graph, batch_loss))
    total_iterations += 1
    if total_iterations % 20 == 1:
        print ("These iteration=" + str(total_iterations) + ") took: " + str(datetime.now() - time_before) + ", with loss of " + str(batch_loss))
        iteration_path = output_dir + 'Iterations_graph_' + str(total_iterations) + '.mat'
        savemat(iteration_path, dict(loss_graph=loss_graph))
    if total_iterations % 1000 == 0:  # Save the model at every 1000th iterations.
       weights_path = output_dir + 'weightsAnomalyL1L2_' + str(total_iterations) + '.mat'
       save_model(model, model_path, weights_path)