๐Ÿ€ TIL/WIL ํŠน๊ฐ• by ๊ฐ•๋ฏผ์ฒ  ํŠœํ„ฐ

til์€ ๋‚ด๊ฐ€ ์ด๊ฑธ ์•„๋Š” ๊ฒƒ์„ ์ฆ๋ช…ํ•˜๊ธฐ ์œ„ํ•ด์„œ, ์•ˆ๊นŒ๋จน๊ธฐ ์œ„ํ•œ ํ•˜๋‚˜์˜ ๋„๊ตฌ
์„ฑ์žฅ๊ณผ์ •์„ ๋ณผ ์ˆ˜ ์žˆ์–ด์„œ ์—…๋ฌด ์„ฑํ–ฅ, ์„ฑ์‹ค๋„, ํ˜‘์—…๊ฐ€๋Šฅ์„ฑ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค

  • ์–ด๋–ค ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ๋Š”์ง€
  • ๋‚ด๊ฐ€ ๋ญ˜ ๋ชฐ๋ž๋Š”์ง€(๋‚ด๊ฐ€ ์‹œ๋„ํ•ด ๋ณธ ๊ฒƒ)โญ
  • ์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐํ–ˆ๋Š”์ง€
  • ๋ญ˜ ์•Œ์•˜๋Š”์ง€โญ

wil์€ til์˜ ํšŒ๊ณ 

  • ์ด๋ฒˆ์ฃผ ์•Œ๊ฒŒ ๋œ ์  ์ •๋ฆฌ
  • ๋‹ค์Œ์ฃผ ๋ชฉํ‘œ ์„ธ์šฐ๊ธฐ
  • ์ด๋ฒˆ์ฃผ ๋ชฉํ‘œ ๋‹ฌ์„ฑ์—ฌ๋ถ€

๐Ÿข๊ฑฐ๋ถ์ด๋ฐ˜

ํ•ด์‹ฑ๋ผ์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ €์žฅ๋œ ์• ๋ž‘ ๋กœ๊ทธ์ธํ•  ๋•Œ ์ž…๋ ฅํ•œ ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ํ•ด์‹ฑ๋œ ๊ฐ’์ด๋ž‘ ๋น„๊ตํ•ด์„œ ๋กœ๊ทธ์ธํ•˜๋Š” ๊ฒƒ


๐Ÿ’ฃdjango timeattack

1.ํ”„๋กœ์ ํŠธ ํด๋”๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ฐ€์ƒ ํ™˜๊ฒฝ์„ ์ƒ์„ฑ ๋ฐ ์‹คํ–‰ ํ•ด์ฃผ์„ธ์š”
2. django ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•˜๊ณ ,requirements.txt์— ์„ค์น˜ ๋œ ํŒจํ‚ค์ง€๋ฅผ ์ €์žฅํ•ด์ฃผ์„ธ์š”
3. sparta๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ django ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•ด์ฃผ์„ธ์š”
4. github์— ์ƒˆ๋กœ์šด ๋ฆฌํฌ์ง€ํ† ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•ด์ฃผ์„ธ์š”
5. .gitignore ์„ค์ • ํ›„ sparta ํ”„๋กœ์ ํŠธ๋ฅผ ํ‘ธ์‹œํ•ด์ฃผ์„ธ์š”
6. user๋ผ๋Š” ์ด๋ฆ„์˜ ๋ธŒ๋žœ์น˜๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ด๋™ํ•ด์ฃผ์„ธ์š”
7. user ์•ฑ์„ ๋งŒ๋“ค๊ณ  settings.py์— ๋“ฑ๋กํ•ด์ฃผ์„ธ์š”
8. ์•„๋ž˜ ๋‚ด์šฉ์„ ์ฐธ๊ณ ํ•ด User ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•ด์ฃผ์„ธ์š”

  • AbstractUser ์ƒ์†
  • ํ•ธ๋“œํฐ๋ฒˆํ˜ธ, ์ฃผ์†Œ ํ•„๋“œ ์ถ”๊ฐ€
  1. ์ฒจ๋ถ€ ๋œ signup.html ํ…œํ”Œ๋ฆฟ์„ ํ™œ์šฉํ•ด /signup ๊ฒฝ๋กœ๋กœ ์ ‘์†ํ•  ๊ฒฝ์šฐ ํšŒ์›๊ฐ€์ž… ํŽ˜์ด์ง€๋ฅผ ๋„์›Œ์ฃผ๊ณ  ํšŒ์›๊ฐ€์ž… ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•ด์ฃผ์„ธ์š”(ํšŒ์›๊ฐ€์ž… ์‹œ ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€๋กœ redirection ํ•ด์ฃผ์„ธ์š”)
  2. ์ฒจ๋ถ€ ๋œ login.html ํ…œํ”Œ๋ฆฟ์„ ํ™œ์šฉํ•ด /login ๊ฒฝ๋กœ๋กœ ์ ‘์†ํ•  ๊ฒฝ์šฐ ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€๋ฅผ ๋„์›Œ์ฃผ๊ณ  ๋กœ๊ทธ์ธ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•ด์ฃผ์„ธ์š”(๋กœ๊ทธ์ธ ์‹œ home ํŽ˜์ด์ง€๋กœ redirection ํ•ด์ฃผ์„ธ์š”)
  3. ์ฒจ๋ถ€ ๋œ home.html ํ…œํ”Œ๋ฆฟ์„ ํ™œ์šฉํ•ด /home ๊ฒฝ๋กœ๋กœ ์ ‘์†ํ•  ๊ฒฝ์šฐ ๋ฉ”์ธ ํŽ˜์ด์ง€๋ฅผ ๋„์›Œ์ฃผ๊ณ  ๋กœ๊ทธ์ธ ํ•˜์ง€ ์•Š์€ ์‚ฌ์šฉ์ž๋Š” ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€๋กœ ์ ‘์†ํ•˜๋„๋ก ํ•ด์ฃผ์„ธ์š”
  4. ์ž‘์—… ๋‚ด์šฉ์„ user ๋ธŒ๋žœ์น˜์— ํ‘ธ์‹œํ•˜๊ณ , ๋ฉ”์ธ ํ”„๋ Œ์น˜์— Pull Request๋ฅผ ์š”์ฒญํ•ด์ฃผ์„ธ์š”
  5. main ๋ธŒ๋žœ์น˜์—์„œ user ๋ธŒ๋žœ์น˜์˜ ์ž‘์—… ๋‚ด์šฉ์„ ๋จธ์ง€ํ•ด์ฃผ์„ธ์š”

โŒ์˜ค๋ฅ˜

  • signup.html
<form method="post" action ="/signup/">

Page not found (404)
Request Method: POST
Request URL: http://127.0.0.1:8000/signup/signup/

โžœ action ์•ˆ์— ๋“ค์–ด๊ฐ€๋Š” url์€ /๊ฐ€ ์•ž์— ๊ผญ ์žˆ์–ด์•ผํ•œ๋‹ค ๊ทธ๋ฆฌ๊ณ  ๊ผญ ์ƒˆ๋กœ๊ณ ์นจ ํ•˜์ž..
์•„๋‹ˆ๋ฉด ์ฃผ์†Œ๋ฅผ ๋ชฝ๋•… ๋‹ค ์ ์–ด์ค˜๋„ ๋œ๋‹ค โžœ http://127.0.0.1:8000/signup

  • views.py
from django.shortcuts import render,redirect
from .models import User
from django.contrib.auth import login, authenticate

# Create your views here.
def signup(request):
    if request.method == 'GET':
        return render(request,'signup.html')
        #์ง€๊ธˆ์€ ์กฐ๊ฑด๋ฌธ ์•ˆ์— ๋ฆฌํ„ด์ด์žˆ์œผ๋‹ˆ๊นŒ ๋’ค์—์žˆ๋Š” ์กฐ๊ฑด๋ฌธ์ด if์ด๋“  elif๋“  ์ƒ๊ด€์—†์ง€๋งŒ
        # ๋ฆฌํ„ด์ด ์—†์œผ๋ฉด if๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒŒ ์ปจ๋ฒค์…˜
    elif request.method == 'POST':
        user = User()
        user.username = request.POST.get('username')
        # password = request.POST.get('password') ์•”ํ˜ธํ™”๊ฐ€ ์•ˆ๋จ
        user.set_password(request.POST.get('password'))
        user.phone = request.POST.get('phone')
        user.address = request.POST.get('address')
        # User.objects.create_user(username=username,password=password,phone=phone,address=address)
        # -> ์–˜๋Š” username, password๋งŒ ์ž…๋ ฅํ•ด๋„ ๋œ๋‹ค
        user.save()
        return redirect('/login/') # ์•ž์—๋„ ์Šฌ๋ž˜์‹œ ์žˆ์–ด์•ผํ•จ
        #redirect('user:login')๋„ ๊ฐ€๋Šฅ ๋ชจ๋ธ์˜ url name์„ ๊ฐ€์ง€๊ณ  ๋ฆฌ๋‹ค์ด๋ ‰ํŠธํ•จ

def log_in(request):
    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')
        user = authenticate(request,username=username,password=password)
        if user is not None:
            login(request,user)
            return redirect('/home')
    elif request.method == 'GET':
        return render(request,'login.html')

โŒlogin() expected 1 arguments but got2
โžœ ๋‚ด์žฅํ•จ์ˆ˜ login ์„ ๋ถˆ๋Ÿฌ์™€์•ผํ•˜๋Š”๋ฐ ๋‚ด๊ฐ€ ์ •์˜ํ•œ ๋กœ๊ทธ์ธํ•จ์ˆ˜๋ž‘ ์ด๋ฆ„์ด ๊ฐ™์•„์„œ ์˜ค๋ฅ˜ ๋ฐœ์ƒ ์ด๋ฆ„์ด ๊ฐ™์€์ง€ ๊ผญ ์ฒดํฌํ•˜์žใ…

def home(request):
    user= request.user.is_authenticated
    #if request.user.is_anonymous๋Š” ๋ฐ˜๋Œ€
    if user:
        return render(request,'home.html')
    else:
        return redirect('/login/') # ํŽ˜์ด์ง€ ์ด๋™์„ ํ•˜๋ผ๊ณ  ํ–ˆ์œผ๋‹ˆ๊นŒ redirect
        # return render(request,'login.html') # ์ฃผ์†Œ๊ฐ€ ์•ˆ๋ฐ”๋€œ

https://github.com/hyojine/1014test.git


โš™ ๋จธ์‹ ๋Ÿฌ๋‹ 4์ฃผ์ฐจ

๐Ÿงฐ์‹ค์Šต02

  • ํŒŒ์ผ ์••์ถ• ํ’€๊ธฐ
    !unzip -q fruits.zip #์••์ถ• ํ‘ผ ํŒŒ์ผ๋“ค์ด ์•„๋ž˜ ์ถœ๋ ฅ๋˜์ง€ ์•Š๋„๋กํ•จ
  • ์ด๋ฏธ์ง€ ์ฆ๊ฐ•๊ธฐ๋ฒ•(ImageDataGenerator ์‚ฌ์šฉ-์•Œ์•„์„œ ๋กœ๋“œํ•˜๋„๋ก)
    flow_from_directory ๋ฉ”์†Œ๋“œ: ๋ฐ์ดํ„ฐ์…‹์ด ์ €์žฅ๋œ ํด๋”์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์˜ด
    ๋ถ„๋ฅ˜๋ณ„๋กœ ํด๋”์•ˆ์˜ ์ด๋ฏธ์ง€๋“ค์„ ์ฝ์–ด์˜ฌ ์ˆ˜ ์žˆ๋‹ค
    seed=2021 : ๋žœ๋ค์‹œ๋“œ ์–ด๋–ป๊ฒŒ ์„ž์„๊ฒƒ์ธ๊ฐ€
    class_mode='categorical':๋‹คํ•ญ๋ถ„๋ฅ˜/ ์ด์ง„ ๋ถ„๋ฅ˜๋ฉด binary
    ๊ฒฐ๊ณผ: Found 67692 images belonging to 131 classes.
  • ์ „์ดํ•™์Šต
    base_model = back_bone_model
    include_top=False : ์ถœ๋ ฅ ๋ ˆ์ด์–ด๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š๊ฒ ๋‹ค
    = ๋‚ด ๋งˆ์Œ๋Œ€๋กœ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ”๊ฟ”๋ณด๊ฒ ๋‹ค

โžœ ์•„๋ž˜ ์ฝ”๋“œ/ ์จ๋จธ๋ฆฌ์˜ dropout~dense_1๊นŒ์ง€๊ฐ€ ์ถœ๋ ฅ ๋ ˆ์ด์–ด๋ฅผ ๋งˆ์Œ๋Œ€๋กœ ํŠœ๋‹ํ•ด์ฃผ๋Š” ๋ถ€๋ถ„

x = base_model.output
x = Dropout(rate=0.25)(x)
x = Dense(256, activation='relu')(x)
output = Dense(131, activation='softmax')(x)

softmax: ๋‹คํ•ญ๋ถ„๋ฅ˜ - categorical_crossentropy๋ž‘ ์„ธํŠธ
-ํ•™์Šต
callbacks=[ ModelCheckpoint('model.h5', monitor='val_acc', verbose=1, save_best_only=True)] : ์ฝœ๋ฐฑ์— ๋ชจ๋ธ์ฒดํฌํฌ์ธํŠธ๋ฅผ ์ •์˜ํ•˜๊ฒŒ๋˜๋ฉด model.h5์— ๋ชจ๋ธ์˜ ์›จ์ดํŠธ์™€ ๋„คํŠธ์›Œํฌ ๊ตฌ์กฐ๋ฅผ ์ €์žฅํ•˜๊ฒŒ ๋œ๋‹ค ๋ชจ๋‹ˆํ„ฐ๋Š” ๋ฒจ๋ฆฌ๋ฐ์ด์…˜ ์–ดํ๋Ÿฌ์‹œ๊ฐ€ ๊ฐ€์žฅ ๋†’์€ ๊ฒƒ ํ•˜๋‚˜๋งŒ(save_best_only=True)์„ ์ €์žฅํ•˜๊ฒŒ ๋œ๋‹ค verbose=1: ์ €์žฅํ•˜๋ฉด ํ–ˆ๋‹ค๊ณ  ์ถœ๋ ฅํ•ด๋ผ
-ํ…Œ์ŠคํŠธ
argmax: ์›ํ•ซ์ธ์ฝ”๋”ฉ๋œ ๊ฐ’์„ ์›๋ž˜ ๋ผ๋ฒจ๊ฐ’์œผ๋กœ ๋˜๋Œ๋ฆด ๋•Œ ์‚ฌ์šฉ
classes[np.argmax(test_label)]: ํด๋ž˜์Šค์˜ ์ด๋ฆ„์„ ๋ฝ‘์•„๋ƒ„
ground truth: ์ •๋‹ต๊ฐ’
PR: ์˜ˆ์ธก๊ฐ’

๐Ÿงฐ์ˆ™์ œ

  • ์ด๋ฏธ์ง€ ์ฆ๊ฐ•๊ธฐ๋ฒ• ์‚ฌ์šฉํ•  ๋•Œ flow_from_directory๋กœ ์ž๋™์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์˜ฌ ๋•Œ ํด๋ž˜์Šค๋กœ ๋ถ„๋ฅ˜๋œ ํด๋”๋“ค์ด ์žˆ๋Š” ๊ฒฝ๋กœ๊นŒ์ง€์˜ ํด๋” ์ด๋ฆ„์„ ๋‹ค ์ ์–ด์ค˜์•ผํ•œ๋‹ค!
    ์ฒ˜์Œ์— seg_train๋งŒ ์ ์—ˆ๋”๋‹ˆ class๊ฐ€ 1๊ฐœ๋กœ ๋‚˜์™€์„œ ๋‹นํ™ฉํ•จ๐Ÿ˜ฎ

  • โ“์›ํ•ซ์ธ์ฝ”๋”ฉ ์–ด๋””์„œ ํ•œ๊ฑฐ์ง€..?
  • ์ „์ดํ•™์Šต์—์„œ ๋ ˆ์Šค๋„ท์„ ์‚ฌ์šฉํ•ด๋ด„
from tensorflow.keras.applications import ResNet50

input = Input(shape=(224, 224, 3))

base_model = ResNet50(weights='imagenet', include_top=False, input_tensor=input, pooling='max')
  • โŒํ•™์Šต์—์„œ ์˜ค๋ฅ˜๋‚จ... ์™œ๋ƒ๋ฉด.. ๋‚ด๊ฐ€ ์ „์ดํ•™์Šต์—์„œ ๋ผ๋ฒจ๊ฐ’(์ถœ๋ ฅ ๋ผ๋ฒจ ๊ฐฏ์ˆ˜)์„ ์•ˆ๋ฐ”๊ฟ”์ค˜์„œ..ใ…‹ใ…‹ใ…‹

output = Dense(131, activation='softmax')(x)

โžœ ์—ฌ๊ธฐ์„œ 131์€ ์ด์ „ ์‹ค์Šต์—์„œ ์“ฐ๋˜ ๊ฐ’์ธ๋ฐ ๊ทธ๋Œ€๋กœ ์“ฐ๋ฉด ์–ด๋–กํ•จ.. ๋ฏธ์ณค๋‚˜๋ด.. 6์œผ๋กœ ๋ฐ”๊พธ๊ณ  ํ•ด๊ฒฐ โ—โ—

try to check the number of classes, which will input to the FCC.
predictions = Dense(len(folders),name='output', activation="softmax")(x)

check the len(folders) to be correct


๐Ÿ˜Ž์˜ค๋Š˜ ๋ฐฐ์šด ๊ฒƒ

์ž˜ ๋ชจ๋ฅด๊ฒ ์œผ๋ฉด ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ์ž˜ ์ฐพ์•„๋ณด์ž ์ ค ์ •ํ™•ํ•จ
https://docs.djangoproject.com/en/4.1/topics/auth/default/

profile
looooggi

0๊ฐœ์˜ ๋Œ“๊ธ€