paolo626 / 3DCNN-with-keras

This is code for 3DCNN model base UCF101 dataset. it fix other project min error: https://github.com/kcct-fujimotolab/3DCNN
38 stars 6 forks source link

pritning the output results #2

Open Symbadian opened 3 years ago

Symbadian commented 3 years ago

Hey @liupeng678,

thanx for the code pal and I gave you a well deserved star! i manipulated the code and was wondering how did you print out the results via the graph?

If the print configuration is located in the 3dccn.py file at the bottom, well I am not sure why I am experiencing an error!! I see the configurations but for some odd reason the graph details is not being printed to demosntrate the output's results!

I ran this file configuration as well and I am certain the fault is mine own!??!! Can you help me understand my errors so that I can understand the structure of the output, please?

CODE ''' import argparse import os import matplotlib matplotlib.use('AGG') import matplotlib.pyplot as plt import numpy as np from keras.datasets import cifar10 from keras.layers import (Activation, Conv3D, Dense, Dropout, Flatten, MaxPooling3D) from keras.layers.advanced_activations import LeakyReLU from keras.losses import categorical_crossentropy from keras.models import Sequential from keras.optimizers import Adam from keras.utils import np_utils from keras.utils.vis_utils import plot_model from sklearn.model_selection import train_test_split

import videoto3d from tqdm import tqdm

def plot_history(history, result_dir): plt.plot(history.history['accuracy'], marker='.') plt.plot(history.history['val_accuracy'], marker='.') plt.title('model accuracy') plt.xlabel('epoch') plt.ylabel('accuracy') plt.grid() plt.legend(['acc', 'val_acc'], loc='lower right') plt.savefig(os.path.join(result_dir, 'model_accuracy.png')) plt.close()

plt.plot(history.history['loss'], marker='.')
plt.plot(history.history['val_loss'], marker='.')
plt.title('model loss')
plt.xlabel('epoch')
plt.ylabel('loss')
plt.grid()
plt.legend(['loss', 'val_loss'], loc='upper right')
plt.savefig(os.path.join(result_dir, 'model_loss.png'))
plt.close()

def save_history(history, result_dir): loss = history.history['loss'] acc = history.history['accuracy'] val_loss = history.history['val_loss'] val_acc = history.history['val_accuracy'] nb_epoch = len(acc)

with open(os.path.join(result_dir, 'result.txt'), 'w') as fp:
    fp.write('epoch\tloss\tacc\tval_loss\tval_acc\n')
    for i in range(nb_epoch):
        fp.write('{}\t{}\t{}\t{}\t{}\n'.format(
            i, loss[i], acc[i], val_loss[i], val_acc[i]))

def loaddata(video_dir, vid3d, nclass, result_dir, color=False, skip=True): files = os.listdir(video_dir)

X = []
labels = []
labellist = []

pbar = tqdm(total=len(files))

for filename in files:
    print(filename)
    pbar.update(1)
    if filename == '.DS_Store':
        continue
    namelist = os.path.join(video_dir, filename)
    files2 = os.listdir(namelist)
    for  files3 in  files2:
        name = os.path.join(namelist,files3)
        #print("ok")
        print("dir is ",name)
        label = vid3d.get_UCF_classname(filename)
        if label not in labellist:
            if len(labellist) >= nclass:
                continue
            labellist.append(label)
        labels.append(label)

        X.append(vid3d.video3d(name, color=color, skip=skip))

pbar.close()
with open(os.path.join(result_dir, 'classes.txt'), 'w') as fp:
    for i in range(len(labellist)):
        fp.write('{}\n'.format(labellist[i]))

for num, label in enumerate(labellist):
    for i in range(len(labels)):
        if label == labels[i]:
            labels[i] = num
if color:
    return np.array(X).transpose((0, 2, 3, 4, 1)), labels
else:
    return np.array(X).transpose((0, 2, 3, 1)), labels

def main(): parser = argparse.ArgumentParser( description='simple 3D convolution for action recognition') parser.add_argument('--batch', type=int, default=128) parser.add_argument('--epoch', type=int, default=100) parser.add_argument('--videos', type=str, default='UCF101', help='directory where videos are stored') parser.add_argument('--nclass', type=int, default=101) parser.add_argument('--output', type=str, required=True) parser.add_argument('--color', type=bool, default=False) parser.add_argument('--skip', type=bool, default=True) parser.add_argument('--depth', type=int, default=10) args = parser.parse_args()

img_rows, img_cols, frames = 32, 32, args.depth
channel = 3 if args.color else 1
fname_npz = 'dataset_{}_{}_{}.npz'.format(
    args.nclass, args.depth, args.skip)

vid3d = videoto3d.Videoto3D(img_rows, img_cols, frames)
nb_classes = args.nclass
if os.path.exists(fname_npz):
    loadeddata = np.load(fname_npz)
    X, Y = loadeddata["X"], loadeddata["Y"]
else:
    x, y = loaddata(args.videos, vid3d, args.nclass,
                    args.output, args.color, args.skip)
    X = x.reshape((x.shape[0], img_rows, img_cols, frames, channel))
    Y = np_utils.to_categorical(y, nb_classes)

    X = X.astype('float32')
    np.savez(fname_npz, X=X, Y=Y)
    print('Saved dataset to dataset.npz.')
print('X_shape:{}\nY_shape:{}'.format(X.shape, Y.shape))

# Define model
model = Sequential()
model.add(Conv3D(32, kernel_size=(3, 3, 3), input_shape=(
    X.shape[1:]), padding='same'))
model.add(Activation('relu'))
model.add(Conv3D(32, kernel_size=(3, 3, 3), padding='same'))
model.add(Activation('softmax'))
model.add(MaxPooling3D(pool_size=(3, 3, 3), padding='same'))
model.add(Dropout(0.25))

model.add(Conv3D(64, kernel_size=(3, 3, 3), padding='same'))
model.add(Activation('relu'))
model.add(Conv3D(64, kernel_size=(3, 3, 3), padding='same'))
model.add(Activation('softmax'))
model.add(MaxPooling3D(pool_size=(3, 3, 3), padding='same'))
model.add(Dropout(0.25))

model.add(Flatten())
model.add(Dense(512, activation='sigmoid'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes, activation='softmax'))

model.compile(loss=categorical_crossentropy,
              optimizer=Adam(), metrics=['accuracy'])
model.summary()
plot_model(model, show_shapes=True,
           to_file=os.path.join(args.output, 'model.png'))

X_train, X_test, Y_train, Y_test = train_test_split(
    X, Y, test_size=0.2, random_state=43)

history = model.fit(X_train, Y_train, validation_data=(X_test, Y_test), batch_size=args.batch,
                    epochs=args.epoch, verbose=1, shuffle=True)
model.evaluate(X_test, Y_test, verbose=0)
model_json = model.to_json()
if not os.path.isdir(args.output):
    os.makedirs(args.output)
with open(os.path.join(args.output, 'ucf101_3dcnnmodel.json'), 'w') as json_file:
    json_file.write(model_json)
model.save_weights(os.path.join(args.output, 'ucf101_3dcnnmodel.hd5'))

**loss, acc = model.evaluate(X_test, Y_test, verbose=1)
print('Test loss:', loss)
print('Test accuracy:', acc)
print(history.history.keys())
plot_history(history, args.output)
save_history(history, args.output)**

if name == 'main': main()

''' ERROR ''' usage: 3dcnn.py [-h] [--batch BATCH] [--epoch EPOCH] [--videos VIDEOS] [--nclass NCLASS] --output OUTPUT [--color COLOR] [--skip SKIP] [--depth DEPTH] 3dcnn.py: error: the following arguments are required: --output An exception has occurred, use %tb to see the full traceback. '''

can you assist me, please?

Thanx in advance and great job on this code! it was very useful!

paolo626 commented 3 years ago

Hello, MatParr. I am reading about my final exam. Please let me know if you have fixed it. If not, I will find the detail the next Monday.

Symbadian commented 3 years ago

Hi Liupeng678,

I'm still struggling with this challenge as I'm new to the 3dcnn...I'm uncertain of the cause hence me contacting you..

Please, focus on your success with your exams and after communicate with me.

You can do it...crush those exams Liupeng678...

Standing by....

Regards,

MMGP


From: liupeng678 notifications@github.com Sent: Friday, December 11, 2020 1:32:52 PM To: liupeng678/3DCNN-with-keras 3DCNN-with-keras@noreply.github.com Cc: MatParr mmgp0@hotmail.com; Author author@noreply.github.com Subject: Re: [liupeng678/3DCNN-with-keras] pritning the output results (#2)

Hello, MatParr. I am reading about my final exam. Please let me know if you have fixed it. If not, I will find the detail the next Monday.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/liupeng678/3DCNN-with-keras/issues/2#issuecomment-743194548, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AL7WSHJ3EJZFXWZTAYLSFKDSUINQJANCNFSM4URXB5KA.

paolo626 commented 3 years ago

Hello, MatParr. I have finished the last exam. Did you fix it? hh

paolo626 commented 3 years ago

My Facebook is https://www.facebook.com/peng.liu.1485537. We can talk about somethings there.

Symbadian commented 3 years ago

HI liupeng678,

No didn't fix it, will login and chat with you in a few! Before I leave, I would like to wish you and your family a merry xmas and best wishes...

See you soon.

Regards,

MMGP


From: liupeng678 notifications@github.com Sent: Thursday, December 24, 2020 8:26:41 AM To: liupeng678/3DCNN-with-keras 3DCNN-with-keras@noreply.github.com Cc: MatParr mmgp0@hotmail.com; Author author@noreply.github.com Subject: Re: [liupeng678/3DCNN-with-keras] pritning the output results (#2)

My Facebook is https://www.facebook.com/peng.liu.1485537. We can talk about somethings there.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/liupeng678/3DCNN-with-keras/issues/2#issuecomment-750803799, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AL7WSHN6A3P2S2R3UNPVZQTSWL3MDANCNFSM4URXB5KA.

Symbadian commented 3 years ago

Hi liupeng678,

Happy new year to you and yours, I do hope all is well with you as well as your family in these trying times!

My apologies for getting back to you at this time, I contracted a strain of the dreadful c19 virus and took the precautionary steps for recuperation, hence my silence!

I can meet with you on Wednesday 11am GMT if that is ok with you and if “NOT” you specify a time other than Thursdays!

Details: I managed to run your code which was excellent… But I the graph details is not being display as your’s! I am certain that the fault is mine and would love to get your take on it so that I can evade such in the future.

Thank you loads, Anticipating your response!

Cheers, have a great day mat!

liupeng678 notifications@github.com Date: Thursday, 24 December 2020 at 08:26 To: liupeng678/3DCNN-with-keras 3DCNN-with-keras@noreply.github.com Cc: MatParr mmgp0@hotmail.com, Author author@noreply.github.com Subject: Re: [liupeng678/3DCNN-with-keras] pritning the output results (#2)

My Facebook is https://www.facebook.com/peng.liu.1485537. We can talk about somethings there.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/liupeng678/3DCNN-with-keras/issues/2#issuecomment-750803799, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AL7WSHN6A3P2S2R3UNPVZQTSWL3MDANCNFSM4URXB5KA.

Symbadian commented 3 years ago

Hi Liupeng 867,

I am still at it with your code and I have picked by up my research after some challenges with covid 19! I reached out to you based on your request via Facebook but you had exams, I had to be considerate! I hope that you had success with such as well!

In order to reference your work in my report and provide a so deserving star, I need to understand some of the errors that is hindering my progress from moving forward! I tried changing the input values here but that had no effect on the error:

 # define model model = Sequential() model.add(Convolution2D(32, 3, 3, border_mode='same',input_shape=X.shape[1:])) model.add(Activation('relu')) model.add(Convolution2D(32, 3, 3)) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Convolution2D(64, 3, 3, border_mode='same')) model.add(Activation('relu')) model.add(Convolution2D(64, 3, 3)) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(512)) model.add(Activation('relu')) model.add(Dropout(0.5)) model.add(Dense(nb_classes)) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) model.summary()

I ran the exact procedure as you had it the last time here https://github.com/liupeng678/3DCNN-with-keras but these errrors persist!?!?!?!?!

“”” ValueError: Negative dimension size caused by subtracting 3 from 2 for '{{node conv3d_2/Conv3D}} = Conv3D[T=DT_FLOAT, data_format="NDHWC", dilations=[1, 1, 1, 1, 1], padding="VALID", strides=[1, 1, 1, 1, 1]](Placeholder, conv3d_2/Conv3D/ReadVariableOp)' with input shapes: [?,9,9,2,32], [3,3,3,32,64]. “””

I tried researching this, but base on my limited capacity to understand what went wrong it is a dauting task to understand! Hence me reaching out to you for your assistance, Please! I saw this solution, but this was vague and the actual solution is not clear so that I can solve this error, Please Assist!

“”” By default, Convolution2D (https://keras.io/layers/convolutional/) expects the input to be in the format (samples, rows, cols, channels), which is "channels-last". Your data seems to be in the format (samples, channels, rows, cols). You should be able to fix this using the optional keyword data_format = 'channels_first' when declaring the Convolution2D layer.

model.add(Convolution2D(32, (3, 3), activation='relu', input_shape=(1,28,28), data_format='channels_first'))

“”” Thanx In advance.

Mat! [Grey line]

Buckingham.ac.uk

[Facebook logo] http://facebook.com/unibuckingham [Twitter logo] http://twitter.com/uniofbuckingham [Instagram logo] https://www.instagram.com/uniofbuckingham [Youtube logo] http://youtube.com/UniofBuckingham [Grey line] [The University of Buckingham logo] https://www.buckingham.ac.uk [First for teaching excellence]

The University of Buckingham respects your rights with regards to data privacy and data protection. Please review our privacy noticehttps://www.buckingham.ac.uk/privacy-policy/?utm_source=outlook-signature&utm_medium=email&utm_content=privacy-notice&utm_campaign=email-signature-generic, which informs you how we collect, store, use, share, process and protect your personal information. It also tells you how you can access and update your personal information and make certain choices about how your personal information is used. By communicating with the University, using its websites, making applications or by otherwise giving us your personal information you are accepting the practices described in this Privacy Notice. If you do not agree to this Privacy Notice, please do not give us any of your personal information.

The University continues to operate online during the current National Lockdown but many of our buildings are currently closed. For full guidance see https://www.buckingham.ac.uk/student-life/student-welfare/coronavirus-advice