File size: 5,795 Bytes
c18b721
 
9d4d402
 
 
 
c18b721
 
 
 
60603bc
 
acdb2d7
f478c26
13ce165
 
4f5d096
 
c18b721
 
 
 
a962639
c18b721
 
 
 
 
 
 
 
 
8c7151e
 
 
 
 
 
fa18a17
c18b721
8c7151e
 
 
 
 
fa18a17
 
e773d9a
fa18a17
 
 
 
 
 
 
c18b721
9d4d402
8c7151e
fa18a17
 
c18b721
e773d9a
 
 
 
c18b721
 
 
4f5d096
958fcc3
 
 
 
720d5d2
 
 
c18b721
 
 
 
13ce165
 
63b0414
13ce165
 
cd2efd1
13ce165
c18b721
d2b2be7
 
 
 
 
 
4d6910b
2e7d318
d2b2be7
63b0414
cd2efd1
 
 
 
d2b2be7
 
 
13ce165
fa18a17
13ce165
 
c18b721
38321a8
c18b721
d2b2be7
9d4d402
c18b721
 
13ce165
c18b721
 
 
63b0414
c18b721
 
d2b2be7
 
 
 
 
 
 
 
 
 
3b27ab8
d2b2be7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c18b721
 
 
 
 
08633e0
c18b721
 
513b119
 
c18b721
 
 
845b45b
c18b721
 
 
845b45b
 
9d4d402
c18b721
513b119
 
c18b721
 
 
 
3b27ab8
 
c18b721
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#! /usr/bin/env bash

function image_classifier() {
    abcli_image_classifier $@
}

function abcli_image_classifier() {
    local task=$(abcli_unpack_keyword "$1" help)

    if [ "$task" == "help" ] ; then
        abcli_help_line "$abcli_cli_name image_classifier install" \
            "install image_classifier."
        abcli_help_line "$abcli_cli_name image_classifier list [object_1] [model=object/*saved]" \
            "list [saved/object model object_1]."
        abcli_help_line "$abcli_cli_name image_classifier predict data_1 [name_1] [data=filename/*object/url,model=object/*saved]" \
            "run fashion_mnist saved/object model name_1 predict on filename/object/url data_1."
        abcli_help_line "$abcli_cli_name image_classifier save [name_1] [object_1] [force]" \
            "[force] save image_classifier [in object_1] [as name_1]."
        abcli_help_line "$abcli_cli_name image_classifier train object_1" \
            "train image_classifier on data object_1."

        if [ "$(abcli_keyword_is $2 verbose)" == true ] ; then
            python3 -m image_classifier --help
        fi
        return
    fi

    if [[ $(type -t abcli_image_classifier_$task) == "function" ]] ; then
        abcli_image_classifier_$task ${@:2}
        return
    fi

    if [ "$task" == "install" ] ; then
        conda install -y -c anaconda seaborn
        return
    fi

    if [ "$task" == "list" ] ; then
        local model_name=$2

        if [ -z "$model_name" ] ; then
            ls $abcli_path_git/image-classifier/saved_model
            return
        fi

        local options=$3
        local model_source=$(abcli_option "$options" "model" saved)
        local do_browse=$(abcli_option_get_unpacked "$options" "browser" 0)

        local model_path=$(abcli_huggingface get_model_path image-classifier "$model_name" "$options")

        if [ "$model_source" == "object" ] ; then
            local model_object=$(python3 -c "print('$model_path'.split('/')[-1])")
            abcli_download object $model_object
        fi

        python3 -m image_classifier \
            list \
            --model_path $model_path \
            ${@:4}

        if [ "$do_browse" == 1 ] && [ "$model_source" == "object" ] ; then
            abcli_browser $model_object
        fi

        return
    fi

    if [ "$task" == "save" ] ; then
        abcli_huggingface save \
            image-classifier \
            $(abcli_clarify_arg "$2" image-classifier) \
            ${@:3}
        return
    fi

    abcli_log_error "-fashion_mnist: image-classifier: $task: command not found."
}

function abcli_image_classifier_predict() {
    local data_object=$(abcli_clarify_object "$1")

    local model_name=$2

    local options=$3
    local data_source=$(abcli_option "$options" "data" object)
    local model_source=$(abcli_option "$options" "model" saved)

    if [ "$(abcli_keyword_is $data_object validate)" == true ] ; then
        if [ "$data_source" == "object" ] ; then
            abcli_log_error "-imge-classifier: predict: validation object not found."
            return
        fi

        local data_object="https://upload.wikimedia.org/wikipedia/commons/thumb/8/8b/Claquettes-peto.jpg/1024px-Claquettes-peto.jpg"
        local data_source="url"
    fi

    if [ "$data_source" == "object" ] ; then
        abcli_download object $data_object
    fi


    local model_path=$(abcli_huggingface get_model_path image-classifier "$model_name" "$options")

    if [ "$model_source" == "object" ] ; then
        local model_object=$(python3 -c "print('$model_path'.split('/')[-1])")
        abcli_download object $model_object
    fi

    abcli_log "image_classifier($model_path).predict($data_object): $options"

    if [ ! -f "$abcli_object_root/$data_object/test_images.pyndarray" ] && [ "$data_source" == "object" ] ; then
        python3 -m image_classifier \
            preprocess \
            --infer_annotation 0 \
            --model_path $model_path \
            --objects $abcli_object_root/$data_object \
            --output_path $abcli_object_root/$data_object \
            --purpose predict \
            ${@:4}
    fi

    if [ "$data_source" == "object" ] ; then
        cp -v $abcli_object_root/$data_object/*.pyndarray .
        cp -v $model_path/image_classifier/model/class_names.json .

        python3 -m image_classifier \
            predict \
            --data_path $abcli_object_root/$data_object \
            --model_path $model_path \
            --output_path $abcli_object_path \
            ${@:4}

        abcli_tag set . image_classifier,predict
    else
        local is_url=0
        if [ "$data_source" == "url" ] ; then
            local is_url=1
        fi

        python3 -m image_classifier \
            predict_image \
            --data_path $data_object \
            --is_url $is_url \
            --model_path $model_path \
            --output_path $abcli_object_path \
            ${@:4}  
    fi
}

function abcli_image_classifier_train() {
    local data_object=$(abcli_clarify_object "$1" $abcli_object_name)

    abcli_download object $data_object

    local options=$2
    local do_color=$(abcli_option_int "$options" "color" 0)
    local do_convnet=$(abcli_option_int "$options" "convnet" 0)
    local do_validate=$(abcli_option_int "$options" "validate" 0)

    local extra_args=""
    if [ "$do_validate" == 1 ] ; then
        local extra_args="--epochs 2"
    fi

    abcli_log "image_classifier.train($data_object): $options"

    python3 -m image_classifier \
        train \
        --color $do_color \
        --convnet $do_convnet \
        --data_path $abcli_object_root/$data_object \
        --model_path $abcli_object_path \
        $extra_args \
        ${@:3}

    abcli_tag set . image_classifier,train
}